Javascript Cloud Firestore:TypeError:无法读取属性';参考';未定义的

Javascript Cloud Firestore:TypeError:无法读取属性';参考';未定义的,javascript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Google Cloud Firestore,Google Cloud Functions,Cloud Firestore:TypeError:无法读取未定义的属性“ref” 我正在使用云函数更新Cloud Firestore父集合中的注释编号,因此当添加注释时,云函数可以自动更新注释编号 exports.updateCommentNumbers = functions.firestore .document('postlist/{postlistId}/comments/{commentsID}') .onCreate(event => { const collect

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

我正在使用云函数更新Cloud Firestore父集合中的注释编号,因此当添加注释时,云函数可以自动更新注释编号

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) 在下一个(本地) at/user\u code/node\u modules/firebase functions/lib/cloud functions.js:28:71 at_uuwaiter(/user_code/node_modules/firebase functions/lib/cloud functions.js:24:12) 在cloudFunction(/user\u code/node\u modules/firebase functions/lib/cloud functions.js:82:36) at/var/tmp/worker/worker.js:716:24 在进程中。_tickDomainCallback(internal/process/next_tick.js:135:7)

如图所示,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
});

与其为您重新编写代码,我建议您根据该文档进行更新,或者检查代码的来源以查看是否有更新版本。

谢谢您的回复。我从点击中得到了这个示例,因为我阅读了Cloud Firestore文档的中文版本,它不是更新版本。看起来Firebase的最新函数SDK的代码还没有更新。