[英]How do I delete a document from Firebase 9 js using a query?
I want to delete a doc from 'albums' collection, but only the one, which name property matches props.album.name.我想从“相册”集合中删除一个文档,但只删除名称属性与 props.album.name 匹配的文档。 I would expect this code to work:我希望这段代码能够工作:
const colRef = collection(firestore, 'albums')
const q = query(colRef, where('name', '==', props.album.name))
const querySnapshot = await getDocs(q)
querySnapshot.forEach(doc => {
deleteDoc(doc)
})
but I get an error instead:但我得到一个错误:
Uncaught (in promise) TypeError: right-hand side of 'in' should be an object, got undefined未捕获(承诺)TypeError:“in”的右侧应该是 object,未定义
What am I doing wrong?我究竟做错了什么?
Since you're looping over a QuerySnapshot
each doc
object is a QueryDocumentSnapshot
, while deleteDoc
expects a DocumentReference
.由于您正在遍历QuerySnapshot
每个doc
object 是一个QueryDocumentSnapshot
,而deleteDoc
需要一个DocumentReference
。 To get from the former to the latter, you can call .ref
on the snapshot.要从前者转到后者,您可以在快照上调用.ref
。
So:所以:
querySnapshot.forEach(doc => {
deleteDoc(doc.ref)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.