[英]Cloud Firestore: TypeError: Cannot read property 'ref' of undefined
Cloud Firestore: TypeError: Cannot read property 'ref' of undefined Cloud Firestore:TypeError:无法读取未定义的属性“ ref”
I'm using Cloud Functions to update the comments number in the parent collection of Cloud Firestore, so when a comment added the Cloud Functions can automatically update the comment number. 我正在使用Cloud Functions更新Cloud Firestore父级集合中的注释编号,因此添加注释时,Cloud Functions可以自动更新注释编号。
exports.updateCommentNumbers = functions.firestore
.document('postlist/{postlistId}/comments/{commentsID}')
.onCreate(event =>
{
const collectionRef = event.after.ref.parent;
const countRef = collectionRef.parent.child('comment_number');
//const previousValue = event.data.previous.data();
let increment;
if (event.after.exists() )
{
increment = 1;
}
else
{
return null;
}
return countRef.transaction((current) =>
{
return (current || 0) + increment;
}).then(() =>
{
return console.log('Comments numbers updated.');
});
});
I got error which I don't understand. 我收到了我不明白的错误。 Could you tell me what's wrong?
你能告诉我怎么了吗?
TypeError: Cannot read property 'ref' of undefined at exports.updateCommentNumbers.functions.firestore.document.onCreate.event (/user_code/index.js:46:35) at Object.
TypeError:无法读取Object处export.updateCommentNumbers.functions.firestore.document.onCreate.event(/user_code/index.js:46:35)处未定义的属性'ref'。 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) at next (native) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) at /var/tmp/worker/worker.js:716:24 at process._tickDomainCallback (internal/process/next_tick.js:135:7)
(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)在(本机)在__awaiter的/user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)位于/ var /处的cloudFunction(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)在process._tickDomainCallback处的tmp / worker / worker.js:716:24(内部/process/next_tick.js:135:7)
As show in this upgrade guide the signature of Cloud Functions for Firebase has changes when it switched to v1.0. 如本升级指南中所示 ,当切换到v1.0时,Cloud Functions for Firebase的签名会发生更改。
If you used this before v1.0: 如果您在v1.0之前使用此功能:
exports.dbWrite = functions.firestore.document('/path').onWrite((event) => {
const beforeData = event.data.previous.data(); // data before the write
const afterData = event.data.data(); // data after the write
});
It now is: 现在是:
exports.dbWrite = functions.firestore.document('/path').onWrite((change, context) => {
const beforeData = change.before.data(); // data before the write
const afterData = change.after.data(); // data after the write
});
Instead of rewriting the code for you, I recommend you update it based on that documentation, or check where you got the code from to see if there's an updated version. 建议您根据该文档进行更新,或者检查从中获取代码的位置以查看是否有更新的版本,而不是为您重写代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.