【问题标题】:firebase web - update database where a certain value is foundfirebase web - 更新找到某个值的数据库
【发布时间】:2017-06-11 22:04:42
【问题描述】:

我的数据库如下所示:

我需要更新division_name,其中index_num 为3。 我尝试了以下代码,但它不起作用 -

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

这里采用什么方法?

EDIT1: 我在 chrome 控制台中收到警告:

FIREBASE 警告:使用未指定的索引。考虑将 /divisions 处的 ".indexOn": "index_num" 添加到您的安全规则中以获得更好的性能

EDIT2: 在 firebase 数据库中,districts 节点(?)看起来像:

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

现在我想为某个index_numdivision_index 更新district_name。我使用以下代码:

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 

但控制台显示:

Uncaught Error: No index defined for index_num

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

这最终暗示了我的 edit_districts.php 文件中的以下代码行:

snapshot.forEach(function(snapshot_indiv){

query_update.once 部分内部。forEach 方法似乎造成了问题。

并且安全规则定义为

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

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

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

  }
}

如何摆脱更新数据库的错误?

【问题讨论】:

  • “它不起作用”是什么意思?会发生什么?
  • @martinjakubik,数据库中没有发现变化
  • @martinjakubik,在 OP 中添加了“EDIT1”
  • 该警告会告诉您确切的操作(如果您需要帮助,请搜索它)。但即使有该警告,该操作也应该有效。
  • 快速猜测是您将索引存储为数字,但与字符串进行比较。 (在屏幕截图中很难确定,下次请以文本形式共享 JSON - 您可以通过单击 Firebase 数据库控制台中的导出 JSON 链接来获取)如果确实如此:firebase.database().ref("/divisions") .orderByChild('index_num').equalTo(3)(不带引号)跨度>

标签: firebase firebase-realtime-database updates


【解决方案1】:

您需要为 Firebase 数据库安全规则添加索引。我推荐阅读Firebase documentation on security rules,特别是section on indexes

来自文档的第一个示例:

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

修改为你的数据结构,它看起来像这样:

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

【讨论】:

  • 包括使用数字而不是字符串进行比较的解决方案,以便它成为一个完整的答案。
  • 如何添加规则 - 通过手动编辑“规则”部分下的规则?
  • 看一下'EDIT2',弹出新问题。
猜你喜欢
  • 1970-01-01
  • 2017-09-04
  • 1970-01-01
  • 1970-01-01
  • 2017-04-02
  • 2018-02-11
  • 2019-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多