Javascript 当文档值匹配时删除firestore中的文档
我在firestore收藏了一堆文件。我想归档的是通过“timestamp”属性搜索文档,然后我想完全删除这个文档 我的代码现在看起来像这样:Javascript 当文档值匹配时删除firestore中的文档,javascript,firebase,react-native,google-cloud-firestore,Javascript,Firebase,React Native,Google Cloud Firestore,我在firestore收藏了一堆文件。我想归档的是通过“timestamp”属性搜索文档,然后我想完全删除这个文档 我的代码现在看起来像这样: firebase.firestore().collection("chatrooms").doc(`${chatId}`).collection(`${chatId}`).where("timestamp", "==", timekey).get().then((QuerySnapshot) =
firebase.firestore().collection("chatrooms").doc(`${chatId}`).collection(`${chatId}`).where("timestamp", "==", timekey).get().then((QuerySnapshot) => {
if (!QuerySnapshot.empty) {
QuerySnapshot.forEach((doc)=>{
console.log(doc.data().value)
});
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
});
它还没有删除任何内容,只是返回我的收藏中找到的文档的值,这样我就可以查看.where()调用是否有效。我很难将这个.where()与.delete()组合在一起,我甚至不确定这是否可行。有人知道如何完成这项任务吗?我在你的代码中看不到.delete()
。此外,您不能将.delete()
和.where()
组合在一起。您需要文档ID或对它们的引用。您可以尝试以下方法:
firebase.firestore().collection("chatrooms").doc(chatId).collection(chatId).where("timestamp", "==", timekey).get().then((QuerySnapshot) => {
if (!QuerySnapshot.empty) {
const deleteDocs = []
QuerySnapshot.forEach((doc)=>{
console.log(doc.data().value)
deleteDocs.push(doc.ref.delete())
});
Promise.all(deleteDocs).then(() => {
console.log("Docs delete")
}).catch(e => console.log(e))
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
});
PS:如果只有一个变量,就不需要使用[${}
东西]。
您可以直接将var作为.doc(chatId)
传递,我在代码中看不到.delete()
。此外,您不能将.delete()
和.where()
组合在一起。您需要文档ID或对它们的引用。您可以尝试以下方法:
firebase.firestore().collection("chatrooms").doc(chatId).collection(chatId).where("timestamp", "==", timekey).get().then((QuerySnapshot) => {
if (!QuerySnapshot.empty) {
const deleteDocs = []
QuerySnapshot.forEach((doc)=>{
console.log(doc.data().value)
deleteDocs.push(doc.ref.delete())
});
Promise.all(deleteDocs).then(() => {
console.log("Docs delete")
}).catch(e => console.log(e))
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
});
PS:如果只有一个变量,就不需要使用[${}
东西]。
您可以直接将var作为
.doc(chatId)
传递给此注释,无需添加任何内容、完美的答案和完美的解释。非常感谢。没有什么要补充的评论,完美的答案和完美的解释。非常感谢。