使用Cloud Firestore和Node JS时,Firebase函数会记录错误

时间:2018-04-04 08:34:24

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

Nodejs的Firebase功能不会从存储在Cloud Firestore数据库中的数据中检索结果。给予:

  

未定义user_id的TypeError

NodeJS代码

'use-strict'

const admin = require('firebase-admin');
const functions = require('firebase-functions');
const serviceAccount = require("./serviceAccountKey.json");

admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.firestore.document("Users/{user_id}/Notifications/{notification_id}").onWrite(event => {

const user_id = event.params.user_id;
const notification_id = event.params.notification_id;

console.console.log("User ID: " + user_id + " | Notification ID: " + notification_id);
});

Firebase功能日志中的错误

TypeError: Cannot read property 'user_id' of undefined at 
exports.sendNotification.functions.firestore.document.onWrite.event 
(/user_code/index.js:10: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)

数据库规则

service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true; } } }

1 个答案:

答案 0 :(得分:0)

您似乎正在使用Cloud Functions代码中的firebase-functions模块的1.0版。 API在1.0中已更改。请consult the migration guide了解1.0中的变化。

第一个参数不再有params属性传递给onWrite触发器的回调。它是split into two arguments,一个Change和一个EventContext对象。 EventContext参数具有您应该立即使用的params属性。