簡體   English   中英

從回調函數返回值

[英]Returning a value from callback function

我正在使用on()方法來檢索數據庫中的數據快照,但是我需要能夠存儲此快照值,以便可以使用它來檢索另一個單獨的快照。

我們的數據庫如下所示: Firebase實時數據庫

有一個用於用戶的節點,一個用於設備的單獨節點。 每個用戶都有一個子“設備”,它是與該用戶關聯的設備列表。 我擴展的用戶只有一個設備。

我想做的是存儲此deviceID,然后執行單獨的查詢以在“設備”節點中找到該設備。 這是我的代碼:

let uid = fireBaseUser.uid; 
//get a reference to the database
let database = firebase.database();
let ref = database.ref("users/").child(uid).child("devices"); 
ref.on("value", getData);

然后,回調函數如下所示:

function getData(data)
{ 
  currentDevice = Object.keys(data.val())[0];
  console.log("current device: " + currentDevice); 
}

這只是獲取用戶設備列表中的第一台設備並將其打印到控制台。 我試圖弄清楚如何返回此值,以便在從“設備”樹中獲取數據時可以使用它。 我猜這看起來像這樣:

let deviceRef = database.ref("devices/").child(retrievedValue);
deviceRef.on("value", getData2);

其中recoveredValue是我從第一個查詢獲得的deviceID。

有可能在javascript中執行此操作,還是有更好的方法? 我知道已經問過類似的問題,但是我發現我在網上看到的所有示例確實令人困惑,對我沒有太大幫助。 任何幫助都將不勝感激,因為我對此有些執着。 謝謝!

為了實現這一點,您必須修改回調,如下所示:

function getData(data, callback)
{ 
  currentDevice = Object.keys(data.val())[0];
  console.log("current device: " + currentDevice); 
  callback(currentDevice)
}

然后,我們從代碼內調用回調,如下所示:

let uid = fireBaseUser.uid; 
//get a reference to the database
let database = firebase.database();
let ref = database.ref("users/").child(uid).child("devices"); 
ref.on("value", getData((this_is_the_value_from_inside_callback) => {
  console.log(`your value: ${this_is_the_value_from_inside_callback}`)
});

您也可以嘗試運行此小片段(我使用了PlayCode),以查看更友好的測試環境

somefunction = (data, callback) => {

  console.log(`data: ${data}`)
  data += 100
  console.log(`data: ${data}`)
  callback(data)
}

somefunction(100, (dataReturned) => {
  console.log(`data returned: ${dataReturned}`)
})

您必須了解Promise和異步編程。 這是兩種您可以做的事情:

let uid = fireBaseUser.uid; 
//get a reference to the database
let database = firebase.database();
let ref = database.ref("users/").child(uid).child("devices"); 
ref.once("value").then((data) {
  currentDevice = Object.keys(data.val())[0];
  console.log("current device: " + currentDevice); 
  let deviceRef = database.ref("devices/").child(currentDevice);
  return deviceRef.once("value");
}).then((value) {
  console.log("value is " + value);
})

或使用async / await:

let uid = fireBaseUser.uid; 
//get a reference to the database
let database = firebase.database();
let ref = database.ref("users/").child(uid).child("devices"); 
let data = await ref.once("value")
currentDevice = Object.keys(data.val())[0];
console.log("current device: " + currentDevice); 
let deviceRef = database.ref("devices/").child(currentDevice);
let value = await deviceRef.once("value");
console.log("value is " + value);

我對第二個更加有信心,因為我無需測試即可輸入這些內容。

這些鏈接將有助於您開始學習以下內容: https : //firebase.googleblog.com/2016/01/keeping-our-promises-and-callbacks_76.html https://firebase.google.com/docs/functions/終止功能

編輯:我固定上面的代碼通過更換ononce 但是現在,它不再監聽數據庫中的更改。 要更正代碼以偵聽用戶的設備更改,請執行以下操作:

let uid = fireBaseUser.uid; 
//get a reference to the database
let database = firebase.database();
let ref = database.ref("users/").child(uid).child("devices"); 
ref.on("value", getData);

function getData(data) // may need to place this before the code above
{ 
  currentDevice = Object.keys(data.val())[0];
  console.log("current device: " + currentDevice); 
  let deviceRef = database.ref("devices/").child(currentDevice);

  // no need to listen to this, as a change in one device would fire 
  // for every user. you probably don't want that. 
  deviceRef.once("value", (data) { 
    console.log(data);
  });
}

暫無
暫無

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

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