onCreate处理程序的DocumentSnapshot未定义

时间:2018-12-21 02:44:19

标签: node.js firebase google-cloud-firestore google-cloud-functions

我正在尝试为子集合中的onCreate事件设置触发器。我可以使该事件正常触发,但是由于某些原因,当我尝试访问它时,DocumentSnapshot是未定义的。

我的功能基于以下示例:https://github.com/firebase/functions-samples/blob/master/child-count/functions/index.js

exports.onCreateReview = functions.firestore
.document('spots/{spotId}/reviews/{reviewId}')
.onCreate(
    (snapshot, _) => {
        console.log(snapshot.exists())
        const collectionRef = snapshot.ref.parent
        const countRef = collectionRef.parent.child('num_reviews');

        const promise = countRef.transaction((current) => {
            return current + 1;
        });

        console.log('num_reviews incremented');
        return promise;
    });

当我检查Firebase项目中的日志时,出现TypeError:无法读取未定义的属性

1 个答案:

答案 0 :(得分:0)

快照不是承诺,因为它可以触发多次,而承诺只能解析一次。

以下是我的.onCreate函数之一的片段,您可能会觉得有帮助。

exports.newFeedback = functions.firestore.document("feedback/{feedback}").onCreate(function (snap, context) {
    var feedbackObj = snap.data();
    var msg = feedbackObj.message;
    var subject = "New Feedback: " + msg.substring(0,36);
    var feedbackMsg = {
        to: "ron@hightechtele.com",
        from: "noreply@hightechtele.com",
        subject: subject,
        templateId: "----------------------------",
        substitutionWrappers: ["{{", "}}"],
        substitutions: {
            message: msg,
            senderDisplayName: feedbackObj.sender,
            senderEmail: feedbackObj.senderEmail,
            senderUid: feedbackObj.senderUid
        }
    };
    return sendgrid.send(feedbackMsg);
});