简体   繁体   English

Firebase 云函数。 无法读取未定义 (.ref) 的属性父级

[英]Firebase Cloud Functions. Cannot read property parent of undefined (.ref)

I am trying to deploy the following function to firebase.我正在尝试将以下 function 部署到 firebase。 The function deploys fine, but when the function triggers I get an error: cannot read property 'parent' of undefined . function 部署良好,但是当 function 触发时,我收到一个错误: cannot read property 'parent' of undefined The error occurs in the first line I reference parent.错误发生在我引用父级的第一行。 I used console.log on snapshot and snapshot.ref, and although snapshot exists, snapshot.ref is undefined.我在快照和快照.ref 上使用了console.log,虽然快照存在,但快照.ref 是未定义的。

I have used snapshot.ref.parent in other cloud functions and it is working fine.我在其他云功能中使用了 snapshot.ref.parent,它工作正常。 There are two main difference with this function: (a) it is an onUpdate (I have previously been using onCreate and onDelete) (b) it is an async function.这个 function 有两个主要区别:(a)它是一个 onUpdate(我以前一直使用 onCreate 和 onDelete)(b)它是一个异步 function。

exports.likeRating = functions.database.ref('Ocean/{waveId}/Likes').onUpdate(async (snapshot) =>{
    let likes; let dislikes; let comments; let echoes;

    await snapshot.ref.parent.child('Dislikes').once('value').then(response=>{dislikes = response.val(); return null});
    await snapshot.ref.parent.child('Likes').once('value').then(response=>{likes = response.val(); return null});
    await snapshot.ref.parent.child('Comments').child('CommentsCount').once('value').then(response=>{comments = response.val(); return null});
    await snapshot.ref.parent.child('Echoes').once('value').then(response=>{echoes = response.val(); return null});

    snapshot.ref.parent.child('Rating').set(dislikes+likes+comments+echoes);
    return null;

}

Any ideas as to why I am getting this error?关于我为什么会收到此错误的任何想法? All help is appreciated.感谢所有帮助。

That function will run significantly slower than it needs to as you're waiting for your requests in series, you should await a Promise.all([<Promises>]) instead, also the return null is redundant. function 的运行速度将比它需要的慢得多,因为您正在等待串行请求,您应该等待Promise.all([<Promises>])代替, return null也是多余的。

I'm also not sure why you add everything up each time instead of incrementing the Rating value but maybe I didn't think about it as much as you.我也不确定为什么你每次都加起来而不是增加评级值,但也许我没有像你一样考虑它。

If you look at the docs the signature of the callback is function(non-null functions.Change containing non-null functions.firestore.DocumentSnapshot, optional non-null functions.EventContext)如果您查看文档,则回调的签名是function(non-null functions.Change containing non-null functions.firestore.DocumentSnapshot, optional non-null functions.EventContext)

So the first param ischange which contains before and after which are of type DocumentSnapshot , it is those properties you should be using eg change.after.ref .所以第一个参数是change ,它包含beforeafterDocumentSnapshot类型,它是您应该使用的那些属性,例如change.after.ref

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

相关问题 firebase云函数无法读取未定义的属性“ ref” - firebase cloud functions Cannot read property 'ref' of undefined 无法读取 Firebase 云 Function 中未定义的属性“参考” - Cannot read property 'ref' of undefined in Firebase Cloud Function Firebase函数无法读取未定义的属性“ val” - Firebase functions Cannot read property 'val' of undefined Cloud Firestore:TypeError:无法读取未定义的属性“ ref” - Cloud Firestore: TypeError: Cannot read property 'ref' of undefined Firebase函数:无法读取未定义的属性“ val” - Firebase Functions: Cannot read property 'val' of undefined 无法读取未定义 Firebase 函数的属性获取 - Cannot read property get of undefined Firebase functions Firebase给出错误:无法读取未定义的属性“ ref” - Firebase giving error : cannot read property 'ref' of undefined 类型错误:无法读取未定义的 Firebase 存储反应的属性“ref” - TypeError: Cannot read property 'ref' of undefined Firebase-storage react 无法读取未定义的属性“ 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