Reputation: 788
Firebase has documented how to handle transactions with Java but not with Nodejs(document)
Due to its structure transactions might return null at first run and unlike in Java, Nodejs doesn't have getValue(). What is returned from transaction is the value itself. So how can I apply the structure above to Nodejs?
In my code the data might be null and if that's the case it must set the count to 1. But when I'm getting "false nulls" I can't figure out what to do.
admin.database().ref(`/matches/${uid}/${matchUID}`)
.transaction(current => {
if (current === null) {
userMatchesRef.child(matchUID).set(1);
}
else {
userMatchesRef.child(matchUID).set(current + 1);
}
})
Upvotes: 0
Views: 2377
Reputation: 599766
Firebase database transactions are almost always initially triggered with a null
for the current
value. You will need to handle this, even when you know for a fact that the value is not null
. Then your callback will be invoked again, and then you'll get a more reasonable value for current
. For an explanation of how Firebase database transactions work, see my answer here: Firebase runTransaction not working - MutableData is null. While it is for Java, the same logic applies across all Firebase SDKs.
For the Firebase documentation on handling transactions in the Node.js SDK, see https://firebase.google.com/docs/database/admin/save-data#section-transactions. But the links from chris' answer will work too, as the Node.js and Web SDK function the same in this respect.
Upvotes: 1
Reputation: 184
https://firebase.google.com/docs/database/web/read-and-write#save_data_as_transactions has node.js documentation that might help. To help you with false nulls please consider adding more information.
Upvotes: 2