firebase How do I listen for errors when accessing the database? Detecting errors when writing a value in JavaScript


There are many reasons a write operation may fail. A frequent one is because your security rules reject the operation, for example because you're not authenticated (by default a database can only be accessed by an authenticated user).

You can see these security rule violations in the console output. But it's easy to overlook these. You can also handle them in your own code and make them more prominently visible, which is especially useful during development (since your JSON, rules and code change often).

To detect a failed write in JavaScript you attach a completion callback to set:

ref.set("My new value").catch(function(error)

Showing an alert like this ensures that it will be very difficult to overlook such an error next time.