Function DocumentReference.update() called with invalid data. Data must be an object, but it was: true

Viewed 24

I have a boolean=false variable in my Firestore collection, and when i click in the button i must then variable changes for true, my code for this is simple but the firebase dont accept. Anyone have a ideia how i change the value of my boolean variable?

this.firestore.doc('lunch/isBag' + this.userID).update(true);

this method is called when i click in the button, but returns that error:

ERROR FirebaseError: Function DocumentReference.update() called with invalid data. Data must be an object, but it was: true (found in document lunch/isBagundefined)

that it's my collection and my variable that I want to change :

Firestore Collection

1 Answers

"Data must be an object" but your code has update(true); that is a boolean. Are you trying to add a field in the document? Try:

this.firestore.doc('lunch/' + this.userID).update({ isBag: true });

There's another problem with this lunch/isBag/USER_ID has 3 path segments that'll point to a "collection" and not a "document". So do check the path of your document once.


As per your database structure you have a collection "lunch" containing documents with user's UID as ID. So the document path is lunch/USER_UID. Now a document is an object in which you have isBag field. You specify an object of fields that you want to update in the update() method as in the answer above.

Related