Reputation: 181
Im trying to update the points of the child : add 40points to the old result points, but I get the error : undefined is not an object (Evaluating 'pointRef.update')
const pointRef=firebase.database().ref("Users").child(firebase.auth().currentUser.uid)
.once("value",(snapshot=>
{
pointRef.update({Points:snapshot.val().Points+40
})
Upvotes: 1
Views: 310
Reputation: 50930
You are using .once()
method on pointRef so it is no longer a DatabaseReference. It is a DataSnapshot now. So you cannot call the .update()
method on it. Instead try using the following code:
const pointRef = firebase.database().ref("Users").child(firebase.auth().currentUser.uid)
pointRef.once("value").then((snapshot) => {
pointRef.update({Points:snapshot.val().Points+40}).then(() => {
console.log("Points updated")
})
})
The better way of doing this:
firebase.database().ref("Users").child(firebase.auth().currentUser.uid)
.set({Points: firebase.database.ServerValue.increment(40)})
Here you don't need to manually fetch, add and update the points field.
Upvotes: 2