TypeError:无法读取未定义的属性“user_id” 在exports.sendNotification.functions.firestore.document.onWrite.event (/user_code/index.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 在__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)
'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.");
});
});
});
});
答案 0 :(得分:0)
当您将值分配给user_id
时。该行是const user_id = event.params.user_id;
event.params未定义
答案 1 :(得分:0)
更改问题行:
.onWrite(( change,context) => {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;