I create a project that uses React + Firebase + Lambda Functions.
I have Firebase code on the front end and I needed a bit of back-end to handle some events. (Prevent users from modifying data in Firebase but allow this data to be updated by the application)
As I use Netlify to deploy my app, I have access to Amazon Lambda Functions with netlify-lambda. (https://www.netlify.com/docs/functions/)
Usually everything just works (mailchimp API, snipcart API etc ...)
But I can not get Firebase working.
I created a service account with the rights to write and read.
Here the code of my lambda function: (Just a test to try to see the users section of the database.)
import firebaseAdmin from 'firebase-admin'
const serviceAccount = require('../utils/FirebaseServiceAccountKey.json')
export function handler (event, context, callback) {
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(serviceAccount),
databaseURL: 'https://sample-3615.firebaseio.com'
})
const db = firebaseAdmin.database()
const ref = db.ref('/users')
let users = {}
ref.once('value', function (snapshot) {
console.log(snapshot.val())
users = snapshot.val()
})
callback(null, {
statusCode: 200,
body: JSON.stringify({ users })
})
}
It returns me : TypeError: rtdb.initStandalone is not a function
.
I also have many Warning like this: Module not found: Error: Can not resolve 'memcpy'
and for other packages too.
My call to the function in a Component:
handleClick = (e) => {
e.preventDefault()
this.setState({loading: true})
fetch('/.netlify/functions/score')
.then(res => res.json())
.then(json => console.log(json.users))
.then(() => this.setState({loading: false}))
}
I'm not sure where the problem comes from. Webpack?