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

时间:2018-04-05 03:20:01

标签: android node.js firebase

我试图测试一个非常简单的nodeJS函数,该函数监视添加到集合中的新文档(Notifications)然后分配该文档的超文档id(userID)和子文档id (notificationID)并将它们打印到控制台。

'use-strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.firestore.document('Users/{userID}/Notifications/{notificationID}').onWrite(event => {

    const to_user = event.params.userID;
    const notification_id = event.params.notificationID;

    console.log("Notification ID: " + notification_id + " Sent to: " + to_user );

});

这是Firebase功能的输出:

TypeError: Cannot read property 'userID' of undefined
    at exports.sendNotification.functions.firestore.document.onWrite.event (/user_code/index.js:8:33)
    at Object.<anonymous> (/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:700:26
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

1 个答案:

答案 0 :(得分:0)

您正在使用新的1.0 SDK,但是为旧版本编写代码。传递给onWrite()的回调的第一个参数现在是Change对象。第二个参数(您在此处未使用)现在是一个EventContext对象,其params属性具有您正在查找的userID通配符值。