我正在使用firestore onUpdate触发器,并尝试获取 更新的文档(字段名称,新值)。
const newValue = change.after.data();
const name = newValue.name;
我希望通知用户,并在其个人资料中添加新标记,例如:
新的出席分数是50
但是当我在通知正文中显示它们时,它会显示:
这是完整的云功能代码段:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
//functions.config().firebase
exports.updateUser = functions.firestore
.document('stuThird/{userId}/stuMarks/{markId}')
.onUpdate((change, context) => {
// Get an object representing the document
// e.g. {'name': 'Marie', 'age': 66}
const newValue = change.after.data();
// ...or the previous value before this update
const previousValue = change.before.data();
// access a particular field as you would any JS property
const name = newValue.name;
var body = ' the new mark of' + name + 'is'+ newValue;
if(newValue){
var message = {
notification: {
title: 'new mark changed',
body: body,
},
topic: 'bebo'
};
}
// Send the message.
return admin.messaging().send(message)
.then((message) => {
return console.log('Successfully sent message:', message);
})
.catch((error) => {
console.error('Error sending message:', error);
});
// perform desired operations ...
});
**
这是数据库: