0

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
        }
    });
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Hudson Kim
  • 416
  • 6
  • 15

1 Answers1

2

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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Now that I have the data of the document how do I get a field from that document? Also the code you gave does not compile in typescript. The second line has to be modified to: var data = (await personFrom).data; – Hudson Kim Apr 19 '20 at 06:15
  • You access fields using properties of the object in the usual JavaScript fashion. You might want to use the documentation. https://firebase.google.com/docs/firestore/query-data/get-data – Doug Stevenson Apr 19 '20 at 06:27
  • Can you show me how? I have looked at the documentation you sent and when I tried it out I received several errors with my code. Please remember I am using typescript and not javascript. The code follows: const personFrom = await db.collection('messages').doc(message.idFrom).get(); const data = (await personFrom).data; var name = data.name; – Hudson Kim Apr 19 '20 at 17:45
  • So what's the error? and what will `console.log(data)` output? – Emil Gi Apr 20 '20 at 09:53
  • @DougStevenson If I try and access the "name" field of the data document with this line of code: var name = data.name; I get this error: Property 'name' does not exist on type '() => DocumentData | undefined'.ts(2339). When I console.log(data) I get: [object Object] – Hudson Kim Apr 21 '20 at 05:21
  • 1
    This has to do with how typescript handles objects and properties, I am not much into it so check [this thread](https://stackoverflow.com/questions/38324949/error-ts2339-property-x-does-not-exist-on-type-y) for additional insights. – Emil Gi Apr 21 '20 at 08:38