0
votes

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)

2

2 Answers

4
votes

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

0
votes

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