Javascript 如何更新子集合并出现错误:[firestore/未找到]?
我需要更新我的子集合上的文档,但我有一个错误: 错误:[firestore/未找到]未找到某些请求的文档 首先,在我的收集团队中选择了好的文档:Javascript 如何更新子集合并出现错误:[firestore/未找到]?,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,我需要更新我的子集合上的文档,但我有一个错误: 错误:[firestore/未找到]未找到某些请求的文档 首先,在我的收集团队中选择了好的文档: firestore() .collection("Teams") .where("tokenTeam", "==", "gvb2j3pcm9") .get() .then(async (querySnapshot) => { if (query
firestore()
.collection("Teams")
.where("tokenTeam", "==", "gvb2j3pcm9")
.get()
.then(async (querySnapshot) => {
if (querySnapshot.empty) {
console.log("no documents found");
} else {
querySnapshot.forEach(async (doc) => {
let Teams = doc._data;
console.log(Teams);
// code below fits in here
})
}
})
我对上面的代码没有错误。然后我用“attente”中的statut调用我的子集合,以选择我要更新的子集合。我的console.log(members)
运行良好
之后,我更新了要更新的所选文档,该文档出现以下错误:
错误:[firestore/未找到]未找到某些请求的文档
这是我的数据模型:
我忘了什么吗?问题在于您在
querySnapshot.forEach
中进行的查询。doc.ref
表示对您在foreach中操作的当前文档的引用,该文档已经是成员列表
文档
因此,您当前在代码中所做的是在原始文档中包含的子集合中查找与单独文档相同的文档,但这不起作用,为了工作,您只需执行以下操作:
doc.ref
.update({
statut: "Validé",
});
如果我理解正确,这就是您的目标:对于给定的团队令牌/ID,将所有具有
“statut”
值“en attente”
的成员更新为“Validé”
如中所述,您只需要使用doc.ref
,而不需要使用doc.ref.collection(“membersList”).doc(members)
。此错误是由于您对名为doc
的变量进行了阴影处理而导致的,这也是您应该正确命名变量的原因
与类似,您可以使用相同的方法搜索请求。当您发现每个文档都要更新时,而不是像您在代码中那样立即更新它,您应该使用来编写单个原子数据库
firestore()
.collection("Teams")
.where("tokenTeam", "==", "gvb2j3pcm9")
.get()
.then(async (matchingTeamsQuerySnapshot) => {
if (matchingTeamsQuerySnapshot.empty) {
console.log("no teams found");
return;
}
// start a batched write
const batch = firestore().batch();
// for each team found, find members with "statut" of "en attente",
// and queue updating "statut" to "Validé"
await Promise.all(
matchingTeamsQuerySnapshot.docs
.map(async (teamDocSnapshot) => {
// const teamData = teamDocSnapshot.data();
// console.log(teamData);
const memberRequestsQuerySnapshot = await teamDocSnapshot.ref
.collection("membersList")
.where("statut", "==", "en attente")
.get();
memberRequestsQuerySnapshot.forEach(memberRequestDoc => {
batch.update(memberRequestDoc.ref, {
statut: "Validé"
});
});
})
);
// update the database all at once
return batch.commit();
})
firestore()
.collection("Teams")
.where("tokenTeam", "==", "gvb2j3pcm9")
.get()
.then(async (matchingTeamsQuerySnapshot) => {
if (matchingTeamsQuerySnapshot.empty) {
console.log("no teams found");
return;
}
// start a batched write
const batch = firestore().batch();
// for each team found, find members with "statut" of "en attente",
// and queue updating "statut" to "Validé"
await Promise.all(
matchingTeamsQuerySnapshot.docs
.map(async (teamDocSnapshot) => {
// const teamData = teamDocSnapshot.data();
// console.log(teamData);
const memberRequestsQuerySnapshot = await teamDocSnapshot.ref
.collection("membersList")
.where("statut", "==", "en attente")
.get();
memberRequestsQuerySnapshot.forEach(memberRequestDoc => {
batch.update(memberRequestDoc.ref, {
statut: "Validé"
});
});
})
);
// update the database all at once
return batch.commit();
})