2016-03-19 47 views
1

更新後成功返回的數據我想在火力更新後返回的數據,但它顯示爲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); 
    } 

回答

3

onComplete callback用於在更新過程中檢查錯誤,不接收數據。

Firebase SDK surfaces update first locally,然後如果成功則觸發onComplete回調。

在你的情況下,你想保存數據,然後使用.on()方法來偵聽更新。執行.update()方法

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

後,它會觸發.on()方法。

+0

非常感謝您的快速回復..現在工作正常..我需要澄清'一次'方法。如果我使用'once'而不是'on'會發生什麼? –

+0

除了連接時只觸發一次而不是每次更新之外,它都是相同的。 –

+1

如何檢測錯誤? – Shardul