Reputation: 486
I am currently trying to use Firebase Cloud Functions in my Flutter application. In my application, I want to retrieve a document field from a certain document when an event happens using typescript. I have done the same thing before in dart however I am unsure of what to do when using typescript. In the code below I attempt to get the field from the document using a method in dart however it fails in typescript with the following error:
Property 'snapshot' does not exist on type 'DocumentReference<DocumentData>'. Did you mean 'onSnapshot'?
I have simplified the code to one cloud function with the most important parts having comments before them. I am trying to get the nickname field from the personFrom documentReference.
const db = admin.firestore();
const fcm = admin.messaging();
export const sendToDevice = functions.firestore
.document('messages/{groupChatId}/{chatId}/{message}')
.onCreate(async (snapshot: { data: () => any; }) => {
const message = snapshot.data();
if (message != null) {
const querySnapshot = await db
.collection('messages')
.doc(message.idTo)
.collection('tokens')
.get();
// **** This is where I am referencing the document where I want to get its fields ****
var personFrom = db.collection('messages').doc(message.idFrom).snapshot();
const tokens = querySnapshot.docs.map((snap: { id: any; }) => snap.id);
const payload: admin.messaging.MessagingPayload = {
notification: {
// **** Right here in the title parameter is where I am attempting to retrieve a specific field of data from the document I mentioned above ****
title: "New Message from" + personFrom["nickname"]+ "!",
body: message.content,
icon: 'your-icon-url',
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
};
return fcm.sendToDevice(tokens, payload);
} else {
return undefined
}
});
Upvotes: 0
Views: 3346
Reputation: 317712
In JavaScript, you use get() on a DocumentReference to fetch a document. It returns a promise that resolves with a DocumentSnapshot object:
const personFrom = await db.collection('messages').doc(message.idFrom).get();
const data = personFrom.data();
Now, data has a JavaScript object that describes the contents of the document.
Upvotes: 2