I am trying to catch the update of a document and send a notification to all users but the catch value I'm having trouble parsing it.
in the console.log() this is the catch data:
{ 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...' }
...
and this is my function:
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);
});
how do I get the latest displayName and the content from the newValue?