Reputation: 598
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?
Upvotes: 0
Views: 292
Reputation: 116
EDIT: Have deleted my previous solution because it introduced new errors as per comments by @fenchai. The crux of the problem is of course dealing with values in typescript, that are possibly null or undefined. Typescript will want you to null check them.
I looked further into this and this SF post had more clarification: https://stackoverflow.com/a/58401023/10303131
As @fenchai notes, you can use the ? operator.
Please read release notes of Typescript, as of late 2019: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html
Items of interest:
Optional Chaining:
// Make x = foo.bar(). If foo null or undefined, x will be undefined.
let x = foo?.bar()
Nullish Coalescing:
// Makes x equal to foo, or if it is null/ undefined, call bar().
let x = foo ?? bar();
From a firebase functions point of view, I would still recommend to anyone that they null check important variables before calling further code, as you will have a chance to clarify important errors, as firebase functions may not always tell you which value is undefined and the root cause of the problem.
Example:
const message = myDocument?.data()?.message;
if (message === null || message === undefined){
console.error("Message is undefined or null");
// Proceed, or return if message vital to function.
}
Upvotes: 1