簡體   English   中英

在Firebase中更新后如何返回成功數據

[英]How to return Success data after update in firebase

我嘗試在Firebase中更新后返回數據,但顯示為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);
    }

onComplete回調用於檢查更新期間的錯誤,而不是接收數據。

Firebase SDK曲面首先在本地更新 ,然后如果更新成功,則會觸發onComplete回調。

在您的情況下,您想要保存數據,然后使用.on()方法監聽更新。

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

執行.update()方法后,它將觸發.on()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM