简体   繁体   English

firebase云函数无法读取未定义的属性“ ref”

[英]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. 当我想从实时数据库更新Cloud Firestore时,我部署了以下代码,但出现错误。

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) TypeError:无法读取cloudFunctionNewSignature(/ user_code / node_modules / firebase-functions / lib / cloudFunction(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)处的cloud-functions.js:105:23)位于/var/tmp/worker/worker.js:733:24处process._tickDomainCallback(内部/进程/next_tick.js:135:7)

It looks like you got the code for a beta version of Cloud Functions for Firebase. 看来您已经获得了Cloud Functions for Firebase Beta版的代码。 The syntax has changed in the 1.0 version. 语法在1.0版本中已更改。 From the documentation on upgrading your Cloud Functions : 有关升级Cloud Functions文档中

or onWrite and onUpdate events, the data parameter has before and after fields. 或onWrite和onUpdate事件,则data参数具有before和after字段。 Each of these is a DataSnapshot with the same methods available in admin.database.DataSnapshot. 其中每个都是DataSnapshot,其管理方法与admin.database.DataSnapshot中的可用方法相同。 For example: 例如:

Before (<= v0.9.1) 之前(<= 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) 现在(> = 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 => { .onUpdate((change, context) => {来声明功能,而不是.onUpdate(event => {
  • use change.after to refer to the data, instead of event.data 使用change.after引用数据,而不是event.data
  • use change.after.ref.once('value') , instead of event.data.ref.once('value') 使用change.after.ref.once('value') ,而不是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 例如,您的代码可能基于的Firestore文档在此处包含最新示例: https : //firebase.google.com/docs/firestore/solutions/presence#updating_globally

Try to change below code, as firebase functions on events have two properties any more. 尝试更改以下代码,因为事件的firebase函数不再具有两个属性。 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 event.data不再存在,而是使用event.val()获取更多信息,并且event具有以下属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 云函数。 无法读取未定义 (.ref) 的属性父级 - Firebase Cloud Functions. Cannot read property parent of undefined (.ref) 无法读取 Firebase 云 Function 中未定义的属性“参考” - Cannot read property 'ref' of undefined in Firebase Cloud Function Cloud Firestore:TypeError:无法读取未定义的属性“ ref” - Cloud Firestore: TypeError: Cannot read property 'ref' of undefined Firebase函数无法读取未定义的属性“ val” - Firebase functions Cannot read property 'val' of undefined Firebase函数:无法读取未定义的属性“ val” - Firebase Functions: Cannot read property 'val' of undefined 无法读取未定义 Firebase 函数的属性获取 - Cannot read property get of undefined Firebase functions 类型错误:无法读取未定义的 Firebase 存储反应的属性“ref” - TypeError: Cannot read property 'ref' of undefined Firebase-storage react Firebase给出错误:无法读取未定义的属性“ ref” - Firebase giving error : cannot read property 'ref' of undefined 无法读取未定义的属性“ ref” - Cannot read property 'ref' of undefined 如何使用 Cloud Functions 解决 Firebase 中的“TypeError:无法读取未定义的属性‘名字’”? - How to solve "TypeError: Cannot read property 'firstname' of undefined" in Firebase with Cloud Functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM