I tried to return data after update in firebase but it is displaying as undefined
test().then((res) => {
alert(res);
});
test() {
var fredNameRef = new Firebase('https://docs-examples.firebaseio.com/samplechat/users/fred/name');
var onComplete = function(error) {
if (error) {
return error;
} else {
return "success";
}
};
fredNameRef.update({ first: 'Wilma', last: 'Flintstone' }, onComplete);
}
The onComplete callback is used to check for errors during the update, not to receive the data.
The Firebase SDK surfaces update first locally, and then if successful it fires the onComplete callback.
In your case you want to save the data and then listen for the update using the .on() method.
var fredNameRef = new Firebase('https://docs-examples.firebaseio.com/samplechat/users/fred/name');
fredNameRef.on('value', function(snap) {
console.log(snap.val());
});
fredNameRef.update({ first: 'Wilma', last: 'Flintstone' });
After the .update() method is executed, it will trigger the .on() method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With