我想更改 firestore 文档集合中名为 boston 的每个州的名称,如何查询 firestore 中的文档列表,然后使用 React Native 将当前州名称更改为新名称?
我试过这个
export const Save = ({ NewName }) => {
var db = firebase.firestore()
var batch = db.batch();
var sfDocRef = db.collection("cities").where("state", "==", "boston");
var sfRe = db.collection("country").doc("SF");
return () => {
batch.update(sfDocRef, {"state": NewName})
batch.update(sfRe, {"state": NewName})
batch.commit()
}
}
但出现此错误
函数 WriteBatch.update() 要求其第一个参数是 DocumentReference,但它是:自定义查询对象
事实上,
sfDocRef
不是DocumentReference
,而是Query
。
get()
方法执行查询,并且对于此查询返回的每个文档,将其添加到批次中。下面的代码就可以解决这个问题:
var batch = db.batch();
var sfRe = db.collection("country").doc("SF");
var sfDocQuery = db.collection("cities").where("state", "==", "boston");
sfDocQuery.get().then(querySnapshot => {
querySnapshot.forEach(doc => {
batch.update(doc.ref, { "state": NewName });
});
//......
batch.update(sfRe, {"state": NewName}); //This one will work, since sfRe is a DocumentReference
//......
return batch.commit()
})
.then(() => {
//The commit() method is asynchronous and returns a Promise
//return for your Save function
})