[英]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 => {
change.after
to refer to the data, instead of event.data
change.after
引用数据,而不是event.data
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.