簡體   English   中英

為什么當引用不存在時,firebase“on”“value”不會運行監聽器功能,但“一次”“值”是什么?

[英]Why does firebase “on” “value” not run the listener function when the reference doesn't exist, but “once” “value” does?

說我正在聽一個小部件:

const ref = firebase.database().ref(`widgets/${widgetId}`);
ref.on('value', function(snapshot){
  console.log(snapshot.val()); // <-- I never see this if ref doesn't exist!
}

如果該小部件不存在,我想知道它。 我希望snapshot.val()返回null 但問題是功能根本沒有運行。 所以我不能檢查null事件。 它只是保持沉默。

奇怪的是,這解決了它:

const ref = firebase.database().ref(`widgets/${widgetId}`);
ref.once('value', function(){});
ref.on('value', function(snapshot){
  console.log(snapshot.val()); // <-- Now is null!
}

現在提供給on的函數與snapshot.val() === null一起運行。 這是一個錯誤還是我做錯了什么?

我需要將Firebase升級到最新版本(3.0.5 - > 3.2.0)

npm install --save firebase

暫無
暫無

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

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