简体   繁体   中英

How to retrieve data from firebase and compare the data with string in Javascript?

I'm not very used to Javascript code and firebase.

So, in firebase I have one parent node where I need to get the data from. This parent has child where each child has two kind of value in it (refer to the image)

在此处输入图像描述

Firstly I need to compare if my "Tanda" value equals to "ada kebakaran". If it's equal, then it start to retrieve the value on "kode".

this "kode" is use as reference to get more data in another parent node.

so far my code like this

function getdata(data){
firebase.database().ref("sensor").on('value', function(snapshot)){
  snapshot.forEach(function(childSnapshot))
  {
    var sensor = childSnapshot.val();
    var keys = Object.keys(sensor);
    for (var i = 0; i < keys.length; i++){
      var k = keys[i];
      var tanda = sensor[k].tanda;
      if(tanda && "ada kebakaran"){
        //get the "kode" here

You need to do the following:

firebase.database().ref("sensor").on('value', function(snapshot)){
  snapshot.forEach(function(childSnapshot))
  {
    var sensor = childSnapshot.val();
    var tanda  = sensor["tanda_kebakaran"];
      if(tanda == "ada kebakaran"){
        //get the "kode" here
        var kode = sensor.kode;
       }
    });

Add a reference to node sensor then iterate using forEach() , childSnapshot.val() will contain all the data under sensor .. Then you can access tanda_kebakaran value using bracket notation, and compare it.

Pardon me if I understood the question differently. From what I understood, here's the code.

admin.firestore().collection("sensor").get().then((data) => {
    let finalData = [];
    data.forEach((doc) => {
        if(doc.tanda_kebakaran === "1")
            finalData.push({
                kode: doc.kode,
                tanda_kebakaran: doc.tanda_kebakaran
            });
    })
})

the finalData array should have the data you need.

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