TypeError: 无法读取未定义的属性"user_id" at exports.sendNotification.functions.firestore.document.onWrite.event (/user_code/索引.js:9:31( 在对象。(/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 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:716:24 在process._tickDomainCallback(内部/进程/next_tick.js:135:7(
'use-strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
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;
return
admin.firestore().collection("Users").doc(user_id).collection("Notifications").doc(notification_id).get().then(queryResult =>{
const from_user_id = queryResult.data().from;
const from_message = queryResult.data().message;
const from_data = admin.firestore().collection("Users").doc(from_user_id).get();
const to_data = admin.firestore().collection("Users").doc(user_id).get();
return Promise.all([from_data, to_data]).then(result => {
const from_name = result[0].data().name;
const to_name = result[1].data().name;
const token_id = result[1].data().token_id;
const payload = {
notification:{
title : "ViewApp",
body : from_message,
icon : "default",
sound : "default",
click_action : "guilherme.viewapp.com.viewapp.TARGETNOTIFICATION"
},
data : {
message : from_message,
from_user_id : from_user_id
}
};
const options = {
priority: "high",
timeToLive: 60 * 60 * 24 //24 hours
};
return admin.messaging().sendToDevice(token_id,payload, options).then(result => {
console.log("Notification Sent.");
});
});
});
});
这是当您将值分配给user_id
时。该行const user_id = event.params.user_id;
event.params 未定义
更改有问题的行:
.onWrite(( change,context) => {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;