user3903484
user3903484

Reputation: 934

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('firebase-admin');
admin.initializeApp(functions.config().firebase);

const firestore = functions.firestore;

exports.onUserStatusChange = functions.database
    .ref('/status/{userId}')
    .onUpdate(event => {

        var db = admin.firestore();


        //const usersRef = firestore.document('/users/' + event.params.userId);
        const usersRef = db.collection("users");
        var snapShot = event.data;

        return event.data.ref.once('value')
            .then(statusSnap => snapShot.val())
            .then(status => {
                if (status === 'offline'){
                    usersRef
                        .doc(event.params.userId)
                        .set({
                            online: false,
                            last_active: Date.now()
                        }, {merge: true});
                }
            })
    });

TypeError: Cannot read property 'ref' of undefined at exports.onUserStatusChange.functions.database.ref.onUpdate.event (/user_code/index.js:18:20) at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20) at /var/tmp/worker/worker.js:733:24 at process._tickDomainCallback (internal/process/next_tick.js:135:7)

Upvotes: 0

Views: 3525

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599956

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 write
  const 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 write
  const 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

Upvotes: 4

Cappittall
Cappittall

Reputation: 3441

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

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

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

Upvotes: 0

Related Questions