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

时间:2018-05-13 23:38:11

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

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

我使用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.');
    });
});

我收到了一些我不明白的错误。你能告诉我出了什么问题吗?

  

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_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       at process._tickDomainCallback(internal / process / next_tick.js:135:7)

1 个答案:

答案 0 :(得分:2)

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

我建议您根据该文档更新代码,或者检查代码的来源,以确定是否有更新版本。