0

Trying to do a basic check if the done element of the document's data is true.

exports.watchTodos = functions.firestore.document('users/{uid}/todos/{docId}')
        .onUpdate(async (snap, context) => {
            if(snap.after.data().done){ 
            console.log('is done') 
        },
}

But the console.log never executes, but if I log snap.after.data().done it returns true.

Gerry
  • 1,159
  • 1
  • 14
  • 29

1 Answers1

0

Assuming that done is a boolean field type inside your document, you can do the following:

exports.watchTodos = functions.firestore.document('users/{uid}/todos/{docId}')
        .onUpdate( (snap, context) => { 

      // Retrieving uid from the paramaters. 
      const uid = context.params.uid;            

      // TODO: retrieve the done field from the document     
      return admin.firestore().collection("users")
      .doc(`${uid}`)
      .get()
      .then(doc => {

        // Data contains all the fields within your document.
        const data = doc.data();
        // Accessing the phone property.
        const isDone = data.done;

        return console.log(isDone); 

      })
      .catch(err => console.error(err))
  );

})

Don't forget to check the documentation, and other Stackoverflow posts for more examples about this.

sllopis
  • 2,292
  • 1
  • 8
  • 13
  • there is an error in the code editor saying: Object is possibly 'undefined'.ts(2532) when data.done is called – Gerry Jun 18 '20 at 01:36
  • I tried const isDone = data?.done; when I try to deploy this the error is: Error: functions predeploy error: Command terminated with non-zero exit code2 – Gerry Jun 18 '20 at 01:41
  • I am using typescript – Gerry Jun 18 '20 at 01:56
  • take a look at [this question](https://stackoverflow.com/questions/40349987/how-to-suppress-error-ts2533-object-is-possibly-null-or-undefined) – Louis C Jun 25 '20 at 22:20