artooras
artooras

Reputation: 6795

Firebase admin SDK database update returns undefined

I'm updating my database with Firebase Admin SDK like so:

admin.database().ref(`clients/${id}`).update({name: 'name'})
.then(snapshot => console.log(snapshot))

However, it logs undefined. Any ideas why this could be?

"firebase-admin": "^9.12.0"

Upvotes: 0

Views: 365

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83058

This is because the Promise returned by the update() method does not contain the snapshot of the node you've just updated.

You need to re-query the RTDB to get the value of the node, as follows:

const dbRef = admin.database().ref(`clients/${id}`);
dbRef.update({name: 'name'})
.then(() => {
   return dbRef.get();
})
.then(snapshot => {
   console.log(snapshot.val());
})

Upvotes: 1

Related Questions