Javascript 从回调函数返回值
我正在使用on()方法检索数据库中的数据快照,但我需要能够存储此快照值,以便使用它检索另一个单独的快照 以下是我们的数据库的外观: 用户有一个节点,设备有一个单独的节点。每个用户都有一个子“设备”,它是与该用户关联的设备列表。我扩展的用户只有一个设备 我试图做的是存储这个deviceID,然后在“Devices”节点中执行单独的查询以查找该设备。下面是我的代码的样子:Javascript 从回调函数返回值,javascript,firebase,firebase-realtime-database,Javascript,Firebase,Firebase Realtime Database,我正在使用on()方法检索数据库中的数据快照,但我需要能够存储此快照值,以便使用它检索另一个单独的快照 以下是我们的数据库的外观: 用户有一个节点,设备有一个单独的节点。每个用户都有一个子“设备”,它是与该用户关联的设备列表。我扩展的用户只有一个设备 我试图做的是存储这个deviceID,然后在“Devices”节点中执行单独的查询以查找该设备。下面是我的代码的样子: let uid = fireBaseUser.uid; //get a reference to the database
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);
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}`)
});
这只是抓取用户设备列表中的第一个设备并将其打印到控制台。我正在想办法
返回此值,以便在从设备树获取数据时使用。我猜,
看起来像这样:
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);
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}`)
});
其中retrievedValue是我从第一个查询中获取的设备ID
有没有可能用javascript实现这一点,或者有更好的方法?我知道已经有人问过类似的问题,但我发现我在网上看到的所有例子都非常混乱,对我帮助不大。任何帮助都将不胜感激,因为我有点被困在这个问题上了。谢谢 为了实现这一点,您必须修改回调,如下所示:
function getData(data, callback)
{
currentDevice = Object.keys(data.val())[0];
console.log("current device: " + currentDevice);
callback(currentDevice)
}
然后我们从代码中调用回调,如下所示:
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);
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}`)
})
您必须学习承诺和异步编程。这里有两种方法可以满足您的需求:
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);
})
或使用异步/等待:
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);
我对第二个更有信心,因为我在没有测试的情况下输入了这些
这些链接将有助于开始学习以下内容:
编辑:我修复了上面的代码,将上的替换为一次
。但是现在它不再监听数据库中的更改。要更正代码以侦听用户的设备更改,请执行以下操作:
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);
});
}
谢谢你的回复!但是,我收到了以下错误:TypeError:data.val不是函数
。我猜数据快照没有正确传递?有什么想法吗?我真的很感激!对不起,迟了回复!我以前从未使用过Firebase,因此我无法向您展示一个实际的示例,说明这在哪里可以工作。我尝试将基本的“回调”功能应用于函数,以演示如何从中返回值。我查看了这个文档,我的第一个建议是在ref.on
中修改getData
,以接受快照和回调。所以它看起来是这样的:ref.on(“value”,getData(snapshot,(callback)=>{})
。请告诉我这是否有帮助感谢您的建议。我尝试了您建议的每种方法,但对于这两种方法,我都得到了错误Query.on失败:使用1个参数调用。至少需要2个。
。好的,我编辑了代码并提供了一个替代方法。仍然没有测试,希望它能工作:)