firebase web - update database where a certain value is found

家住魔仙堡 提交于 2019-12-13 08:29:58

问题


My database looks as below :

I need to update the division_name where the index_num is 3. I tried the following code but it did not work -

    var division_index_found="3";
    var division_name_given="new div";

    var query_update=firebase.database().ref("/divisions")
    .orderByChild('index_num').equalTo(division_index_found);

    query_update.once("child_added", function(snapshot) {
    snapshot.ref.update({ division_name: division_name_given });
    });

What approach to adopt here ?

EDIT1: I get warning in chrome console :

FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "index_num" at /divisions to your security rules for better performance

EDIT2: From the firebase database the districts node (?) looks like :

 "districts" : {
    "-KbVYCSO8wrMoXD3vL81" : {
      "district_name" : "Rangpur",
      "division_index" : "3",
      "index_num" : 2
    },
    "-KbVYHgbWMDMtGsnmvei" : {
      "district_name" : "jessore",
      "division_index" : "3",
      "index_num" : 3
    },
    "-KbVYKtSnPMFDkx9z0cU" : {
      "district_name" : "district 1",
      "division_index" : "3",
      "index_num" : 4
    }
  }

Now I want to update district_name for a certain index_num and division_index. I use the following code :

var district_index="3";
var division_index="3"
var district_index_parsed = parseInt(district_index);

var query_update=firebase.database().ref("/districts")
.orderByChild('index_num').equalTo(district_index_parsed);


 query_update.once("child_added", function(snapshot) {

 snapshot.forEach(function(snapshot_indiv){

        if(parseInt(snapshot_indiv.division_index)==parseInt(division_index)){


    var district_name_again="new district name";        

snapshot_indiv.ref.update({ district_name: district_name_again },function(error){

});

}// end of if division_index compare
    });// end of forEach



});// end of query_update once 

But the console shows :

Uncaught Error: No index defined for index_num

at je.get (firebase-database.js:94)
...
...
 at edit_districts.php:359
...

which ultimately hints at the following line of code in my edit_districts.php file:

snapshot.forEach(function(snapshot_indiv){

inside the query_update.once part.The forEach method seems to make the problem.

And the security rules are defined as

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",

    "divisions": {
      ".indexOn": "index_num"
    },

      "districts": {
      ".indexOn": "index_num"
    },

  }
}

How to get rid of the error to update the database ?


回答1:


You'll need to add an index to your Firebase Database security rules. I recommend reading the Firebase documentation on security rules, specifically the section on indexes.

From the documentation comes this first sample:

{
  "rules": {
    "dinosaurs": {
      ".indexOn": ["height", "length"]
    }
  }
}

Modified to your data structure, it'll look something like this:

{
  "rules": {
    "divisions": {
      ".indexOn": "index_num"
    }
  }
}


来源:https://stackoverflow.com/questions/41873387/firebase-web-update-database-where-a-certain-value-is-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!