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).
ref.set("My new value").catch(function(error) console.error(error); alert(error); });
Showing an alert like this ensures that it will be very difficult to overlook such an error next time.