[英]How to Match Child of a child with data from local storage (Firebase Realtime Database)
My Database structure is like this我的数据库结构是这样的
I have made a qr code scanner which scan the qr then it match the data from Firebase realtime database in the data matches it go on next page and store that qr code value in local storage.我制作了一个二维码扫描仪,它扫描二维码,然后它与来自 Firebase 实时数据库中的数据相匹配,数据与下一页上的 go 相匹配,并将该二维码值存储在本地存储中。 But When I try to mach the data on next page like this:
但是当我尝试像这样处理下一页上的数据时:
var db= app.database() let machine_id = localStorage.getItem('machineid') //output = 8c96aa286f24(Accurate data on console) db.ref('/All_machines/Generator/').orderByKey().equalTo(machine_id).once("value", snapshot => { if (snapshot.exists()){ const userData = snapshot.val(); var snapshot= snapshot.child(machine_id).val(); console.log(userData); //Output: here i didn't get anything not even any error message on console } })
But When i Write the same code after changing database just take out the machine id from generator and write under All_Machines like this但是当我在更改数据库后编写相同的代码时,只需从生成器中取出机器 ID 并像这样在 All_Machines 下写入
and Change my code according to this then my code start working.并根据此更改我的代码,然后我的代码开始工作。 but when my machine id is under generator its not working.
但是当我的机器 ID 在生成器下时,它不起作用。 code:
代码:
var db= app.database() let machine_id = localStorage.getItem('machineid') db.ref('/All_machines').orderByKey().equalTo(machine_id).once("value", snapshot => { if (snapshot.exists()){ const userData = snapshot.val(); var snapshot= snapshot.child(machine_id).val(); console.log(userData); } })
By Running this code i get all the data from Firebase Realtime database通过运行此代码,我从 Firebase 实时数据库中获取所有数据
//Output:- //输出:-
While calling the data from local storage let machine_id = localStorage.getItem('machineid')
my data comes with a extra space like this ' 8c96aa286f24' this is the reason why my code was not working i updated the code like this.从本地存储调用数据时
let machine_id = localStorage.getItem('machineid')
我的数据带有一个额外的空间,例如“8c96aa286f24”,这就是我的代码无法正常工作的原因,我更新了这样的代码。
var machine_id_2 = localStorage.getItem('machineid') var space = machine_id_2.split(" ") var machine_id_1 = space[1] db.ref('/All_machines/Generator/').orderByKey().equalTo(machine_id_1).once("value", snapshot => { if (snapshot.exists()){ const userData = snapshot.val(); var snapshot= snapshot.child(machine_id).val(); console.log(userData); } })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.