问题描述
我的数据库如下所示:
我需要更新index_num
为3的division_name
.我尝试了以下代码,但没有用-
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 });
});
在这里采用什么方法?
我在Chrome控制台中收到警告:
I get warning in chrome console :
FIREBASE警告:使用未指定的索引.考虑在/divisions的安全规则中添加".indexOn":"index_num"以获得更好的性能
FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "index_num" at /divisions to your security rules for better performance
从Firebase数据库中,districts
节点(?)看起来像:
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
}
}
现在我要为特定的index_num
和division_index
更新district_name
.我使用以下代码:
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
但是控制台显示:
Uncaught Error: No index defined for index_num
at je.get (firebase-database.js:94)
...
...
at edit_districts.php:359
...
最终提示我edit_districts.php
文件中的以下代码行:
which ultimately hints at the following line of code in my edit_districts.php
file:
snapshot.forEach(function(snapshot_indiv){
在query_update.once
部分的内部
.forEach
方法似乎引起了问题.
inside the query_update.once
part.The forEach
method seems to make the problem.
安全规则定义为
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"divisions": {
".indexOn": "index_num"
},
"districts": {
".indexOn": "index_num"
},
}
}
如何摆脱更新数据库的错误?
推荐答案
您需要将索引添加到Firebase数据库安全规则中.我建议阅读有关安全规则的Firebase文档,尤其是.
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.
文档中的第一个示例:
{
"rules": {
"dinosaurs": {
".indexOn": ["height", "length"]
}
}
}
修改为您的数据结构,看起来像这样:
Modified to your data structure, it'll look something like this:
{
"rules": {
"divisions": {
".indexOn": "index_num"
}
}
}
这篇关于firebase网站-更新找到特定值的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!