最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to return Success data after update in firebase - Stack Overflow

programmeradmin6浏览0评论

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('');

    var onComplete = function(error) {
      if (error) {
        return error;
      } else {
        return "success";
      }
    };
    fredNameRef.update({ first: 'Wilma', last: 'Flintstone' }, onComplete);
    }

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./samplechat/users/fred/name');

    var onComplete = function(error) {
      if (error) {
        return error;
      } else {
        return "success";
      }
    };
    fredNameRef.update({ first: 'Wilma', last: 'Flintstone' }, onComplete);
    }
Share Improve this question asked Mar 19, 2016 at 18:46 Akash RaoAkash Rao 9283 gold badges12 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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./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.

发布评论

评论列表(0)

  1. 暂无评论