【问题标题】:Cloud Firestore: TypeError: Cannot read property 'ref' of undefinedCloud Firestore:TypeError:无法读取未定义的属性“ref”
【发布时间】:2018-10-23 14:19:34
【问题描述】:

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

我正在使用 Cloud Functions 更新 Cloud Firestore 父集合中的 cmets 编号,因此当添加评论时,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.');
    });
});

我遇到了我不明白的错误。你能告诉我有什么问题吗?

TypeError:无法读取未定义的属性“ref” 在exports.updateCommentNumbers.functions.firestore.document.onCreate.event (/user_code/index.js:46:35) 在对象。 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) 在下一个(本机) 在 /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 在 __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) 在 cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) 在 /var/tmp/worker/worker.js:716:24 在 process._tickDomainCallback (internal/process/next_tick.js:135:7)

【问题讨论】:

    标签: javascript firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    upgrade guide 所示,Cloud Functions for Firebase 的签名在切换到 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
    });
    

    现在是:

    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
    });
    

    我建议您根据该文档更新代码,而不是为您重写代码,或者检查您从哪里获得代码以查看是否有更新版本。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-25
    • 2021-12-04
    • 2021-04-17
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    相关资源
    最近更新 更多