node.js - firebase云函数中更新的firestore文档的变化数据返回undefined
问题描述
我正在使用 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 ...
});
**
这是数据库:
解决方案
推荐阅读
- c - 在 C 中对于后缀和前缀运算符,操作数应该是左值,那么在这些运算符的情况下,左值对于指针变量的实际含义是什么?
- mysql - 为什么 HAVING 可以引用 COUNT(column_name) 而不能引用 column_name 本身?
- python - Pandas 横向和纵向求和聚合
- python - 我想再训练 4 个 Word2vec 模型并对生成的嵌入矩阵进行平均
- python - 如何使用文本标签自定义 Seaborn Heatmap 中的颜色条
- javascript - React 导出上下文返回解析错误:未定义导出“SearchContextConsumer”
- python - 将 python 变量传递给 SQL 查询
- linux - 如何从 Azure 管道连接到私有容器实例?
- javascript - 具有 JSON 服务器的不同唯一键
- php - PHP Scraper 从谷歌航班获取航班价格