[英]How to get database information from Firebase database once
我到處都看過,沒有地方給我滿意的答案。 如何僅在需要時從Firebase數據庫中檢索數據? 我不想同步數據,我只想在需要時獲取數據。 使用after()的各種嘗試完全失敗了,根本沒有讓我接近。 甚至有可能還是Firebase並非為我要做的事情而設計?
編輯:我想做的最小示例:
let ref = firebase.database().ref("location1");
let intendedValue = "Data not Gathered";
ref.once("value").then( function(snapshot) {
intendedValue = snapshot.val();
});
console.log(intendedValue);
我的Firebase數據庫具有以下結構:
projectFolder:
location1:
datapoint1: "dataValue1";
因此,您可能會認為這會記錄“ dataValue1”,但它會記錄“未收集數據”,就像ref.once()從未運行過一樣。
自從
ref.once("value").then( function(snapshot) { intendedValue = snapshot.val(); });
基本上是一個回調,因此在您記錄輸出時,這行intendedValue = snapshot.val();
甚至還沒有運行。
你可以試試這個
ref.once("value").then( function(snapshot) {
intendedValue = snapshot.val();
console.log(intendedValue);
});
function fire_base_db(){
var ref = firebase.database().ref();
ref.on("value", function(snapshot) {
console.log(snapshot.val());
}, function (error) {
console.log("Error: " + error.code);
});}
snapshot.val()包含數據庫的值,您可以將snapshot.val()的值存儲在變量中,並將其發送給另一個函數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.