繁体   English   中英

如何将孩子的孩子与本地存储中的数据匹配(Firebase 实时数据库)

[英]How to Match Child of a child with data from local storage (Firebase Realtime Database)

我的数据库结构是这样的

数据库结构

我制作了一个二维码扫描仪,它扫描二维码,然后它与来自 Firebase 实时数据库中的数据相匹配,数据与下一页上的 go 相匹配,并将该二维码值存储在本地存储中。 但是当我尝试像这样处理下一页上的数据时:

 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 } })

但是当我在更改数据库后编写相同的代码时,只需从生成器中取出机器 ID 并像这样在 All_Machines 下写入

在此处输入图像描述

并根据此更改我的代码,然后我的代码开始工作。 但是当我的机器 ID 在生成器下时,它不起作用。 代码:

 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); } })

通过运行此代码,我从 Firebase 实时数据库中获取所有数据

//输出:-

在此处输入图像描述

从本地存储调用数据时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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM