Skip to content Skip to sidebar Skip to footer

Firebase Cloud Functions Cannot Read Property 'ref' Of Undefined

when i want to update Cloud Firestore from Realtime Database i deployed bellow code and i get error. const functions = require('firebase-functions'); const admin = require('firebas

Solution 1:

It looks like you got the code for a beta version of Cloud Functions for Firebase. The syntax has changed in the 1.0 version. From the documentation on upgrading your Cloud Functions:

or onWrite and onUpdate events, the data parameter has before and after fields. Each of these is a DataSnapshot with the same methods available in admin.database.DataSnapshot. For example:

Before (<= v0.9.1)

exports.dbWrite = functions.database.ref('/path').onWrite((event) => {
  const beforeData = event.data.previous.val(); // data before the writeconst afterData = event.data.val(); // data after the write
});

Now (>= v1.0.0)

exports.dbWrite = functions.database.ref('/path').onWrite((change, context) => {
  const beforeData = change.before.val(); // data before the writeconst afterData = change.after.val(); // data after the write
});

So you will want to use:

  • .onUpdate((change, context) => { to declare the funtcion, instead of .onUpdate(event => {
  • use change.after to refer to the data, instead of event.data
  • use change.after.ref.once('value'), instead of event.data.ref.once('value')

Since it seems that this code is mostly copied from somewhere, I'd recommend getting an updated version from there. For example, the Firestore documentation that your code is likely based on, contains an up-to-date example here: https://firebase.google.com/docs/firestore/solutions/presence#updating_globally

Solution 2:

Try to change below code, as firebase functions on events have two properties any more. So, ref position is:

.onUpdate((event,context) => {
 ....
returnevent.ref.once('value')
...

event.data does not exist anymore, instead event.val() for more info and event has properties like

Post a Comment for "Firebase Cloud Functions Cannot Read Property 'ref' Of Undefined"