简体   繁体   中英

Unable to match child of a child from local storage Firebase RealTime database

My database structure is like this

在此处输入图像描述

And I want to Match the child under Generator with my local storage data if data matches I want to show the details of number present under Generator. but when i am matching the number by calling the number from local storage my code doesn't work but when i right the same value manually in the code my code start working.

code:-

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

I have check the value in local storage and the value is accurate

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

While calling the data from local storage 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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