【发布时间】:2020-08-05 02:57:16
【问题描述】:
我正在尝试捕获文档的更新并向所有用户发送通知,但捕获值我无法解析它。
在 console.log() 中,这是捕获数据:
{ createdAt: Timestamp { _seconds: 1586881980, _nanoseconds: 0 },
messages:
[ { content: 'Un nuevo comienzo para tod@s!\n:)\n????\n:-P\n????',
createdAt: [Object],
displayName: 'Fer...',
photoUrl: 'https://lh3.googleusercontent.com/...',
uid: 'IJchaq...' },
{ content: '????',
createdAt: [Object],
displayName: 'IMP...',
photoUrl: 'https://lh3.googleusercont...' }
...
这是我的功能:
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();
// const db = admin.firestore();
const fcm = admin.messaging();
export const sendToTopic = functions.firestore
.document("chats/{chatsId}")
.onUpdate((change, context) => {
const newValue = change.after.data();
// console.log(newValue);
let latestMessage = newValue.messages[0]; // newValue gives me object is possibly 'undefined'
const payload: admin.messaging.MessagingPayload = {
notification: {
title: "New Message",
body: latestMessage,
icon:
"https://www.dropbox...",
clickAction: "FLUTTER_NOTIFICATION_CLICK",
},
};
return fcm.sendToTopic("globalChat", payload);
});
如何从 newValue 中获取最新的 displayName 和内容?
【问题讨论】:
标签: javascript firebase google-cloud-firestore google-cloud-functions