gpt4 book ai didi

firebase - 要更新的 firestore 批量查询对我不起作用

转载 作者:IT王子 更新时间:2023-10-29 06:58:57 24 4
gpt4 key购买 nike

这里我有一个代码来更新我的 firestore 数据库,这里“m”是一个文档 ID 列表,“a”是一个数字列表,“b”是一个 bool 值列表,列表的所有长度都是相同的。

如果下面的方法不对,请举个批量更新50个文档的例子

void onpressed() async{
alert('updating');
WriteBatch batch= Firestore.instance.batch();
int i=0;
while(b[i]){

batch.updateData(Firestore.instance.collection("allstu").document("17csea").collection("17csea").document(m[i]), {"attendance": a[i]+1});
i++;
}

await batch.commit().then((value){
alert("update successful");
}
).catchError((err){
alert("Something went wrong try again");
});
}

我不知道为什么它不更新我有一个包含 50 个文档 ID 的列表,我正在尝试仅更新列表中的一些文档(由用户选择)。这是通过循环检查更新的同时使用 B[i] 的代码进行更新,这里 B 是 bool 值列表,以便只将需要的数据添加到 batch.ausume a[i ]+1是一个整数

如果上面的方法不对,请举个批量更新50个文档的例子

最佳答案

哦!不,上面的代码是正确的,但我在 while 中使用了 bool 条件,当它为 false 时,整个循环正在终止,而不是替换条件 whith anthor 逻辑条件,并在 while 循环中使用 bool 条件,这将设置你代码 `void onpressed() 异步{

                WriteBatch batch= Firestore.instance.batch();
int i=0;
while(i<m.length){
if(b[i]){
batch.updateData(Firestore.instance.collection("allstu").document("17csea").collection("17csea").document(m[i]), {"attendance": a[i]+1});
print("success");}
i++;
}
batch.commit().then((value){
print("erorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
Navigator.of(context).pop();
}
).catchError((err){
print(err);
Navigator.of(context).pop() ; });
}`

关于firebase - 要更新的 firestore 批量查询对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54162092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com