Skip to content
Advertisement

How to toggle a Boolean field in Firebase 9

I want to toggle a Boolean field value in Firebase 9. I’m using Vue 3

If the value of isFav is true I want to make it false onclick

If the value of isFav is false I want to make it true onclick

It works fine when I set the value of isFav to just false or true. But setting the value as !isFav doesn’t work.

JavaScript

Console

Uncaught ReferenceError: isFav is not defined

Maybe I’m not reading the value of isFav first. But how can I do that?

Advertisement

Answer

You can pass current value of isFav to toggleFav as shown below:

JavaScript

Then use updateDoc() to update the existing document as shown below:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement