简体   繁体   English

Cloud Firestore:TypeError:无法读取未定义的属性“ ref”

[英]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.

相关问题 类型错误:无法读取未定义的属性“ref” - TypeError: Cannot read property 'ref' of undefined Firebase Cloud Function Firestore事务类型错误:无法读取未定义的属性“ forEach” - Firebase Cloud Function Firestore Transaction TypeError: Cannot read property 'forEach' of undefined 类型错误:无法读取未定义的 Firebase 存储反应的属性“ref” - TypeError: Cannot read property 'ref' of undefined Firebase-storage react Firebase 云函数。 无法读取未定义 (.ref) 的属性父级 - Firebase Cloud Functions. Cannot read property parent of undefined (.ref) firebase云函数无法读取未定义的属性“ ref” - firebase cloud functions Cannot read property 'ref' of undefined 无法读取 Firebase 云 Function 中未定义的属性“参考” - Cannot read property 'ref' of undefined in Firebase Cloud Function 无法读取未定义的属性“ ref” - Cannot read property 'ref' of undefined 错误类型错误:无法读取未定义 Ionic/Firestore 的属性“用户名” - ERROR TypeError: Cannot read property 'username' of undefined Ionic/Firestore VueJS & Firestore - 未捕获(承诺)类型错误:无法读取未定义的属性 - VueJS & Firestore - Uncaught (in promise) TypeError: Cannot read property of undefined 未处理的拒绝(TypeError):无法读取反应(firestore)中未定义的属性“setState” - Unhandled Rejection (TypeError): Cannot read property 'setState' of undefined in react (firestore)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM