Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return Success data after update in firebase

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);
    }
like image 964
Akash Rao Avatar asked Jan 23 '26 04:01

Akash Rao


1 Answers

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.

like image 174
David East Avatar answered Jan 24 '26 17:01

David East