![](/img/trans.png)
[英]How to Match Child of a child with data from local storage (Firebase Realtime Database)
[英]Unable to match child of a child from local storage Firebase RealTime database
我的數據庫結構是這樣的
如果數據匹配,我想將生成器下的孩子與我的本地存儲數據匹配我想顯示生成器下存在的數字的詳細信息。 但是當我通過從本地存儲中調用號碼來匹配號碼時,我的代碼不起作用但是當我在代碼中手動修改相同的值時,我的代碼開始工作。
代碼:-
var deviceRef = app.database().ref('/All_machines/Generator/' + localStorage.getItem('machineid'));//machineid = numbers present under genrator. deviceRef.on('child_added', function (data) { //Ambient temperature if (data.key === 'ambT') { console.log(data.val()); //This console not give me any data $('#ambient_temp.value').html(data.val()); temp = data.val() } }) // when I change the localStorage.getItem('machineid') to hardcoded number like this 8c96aa286f24 my code start working //This code is working only by hardcoding the Machineid var deviceRef = app.database().ref('/All_machines/Generator/' + ' 8c96aa286f24');//machineid = numbers present under genrator. deviceRef.on('child_added', function (data) { //Ambient temperature if (data.key === 'ambT') { console.log(data.val()); //but this gives me exact data if i hardcoded the values $('#ambient_temp.value').html(data.val()); temp = data.val() } })
我檢查了本地存儲中的值,該值是准確的
when i call console.log(localStorage.getItem('machineid')) right before var deviceRef = app.database().ref('/All_machines/Generator/' + localStorage.getItem('machineid')); i get the accurate output:- 8c96aa286f24
從本地存儲localStorage.getItem('machineid')
調用數據時,我的數據帶有一個額外的空間,例如' 8c96aa286f24'
這就是我的代碼無法正常工作的原因,我更新了這樣的代碼
var machine_id_2 = localStorage.getItem('machineid') var space = machine_id_2.split(" ") var machine_id_1 = space[1] var deviceRef = db.ref('/All_machines/Generator/' + machine_id_1); deviceRef.on('child_added', function (data) { //Ambient temperature if (data.key === 'ambT') { console.log(data.val()); $('#ambient_temp.value').html(data.val()); temp = data.val() } })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.