简体   繁体   English

如何使用查询从 Firebase 9 js 中删除文档?

[英]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.

相关问题 如何使用带有 get() 的 firebase_admin 查询 firestore 文档 - How do I query a firestore document using firebase_admin with get() 如何通过 flutter 中的文档从 firebase 存储中检索数据? - How do i retrieve data from firebase store by document in flutter? 如何使用 React.js 从 firebase 获取嵌套文档 - How to fetch nested document from firebase using React.js 如何从也有集合的 firebase 文档创建 Object? - How do I create an Object from a firebase document that also has a collection? 如何从 flutter firebase base 中的单个文档读取一次数据(不是实时的)? - How do I read data from a single document in flutter firebase base once (not in real time)? 如果我使用 Firebase/Firestore 查询返回值,如何返回 TableView 数据源值? - How do I return TableView datasource values if I'm using Firebase/Firestore to query the returning value? 如何删除 firebase 和 flutter 和 dart 中所有子集合的文档 - How can I delete a document with all its subcollections in firebase with flutter and dart 如何从 Firebase 中删除记录? - How I can Delete Record from Firebase? React Native 如何获取 Firebase 中特定文档的文档 ID? - How do I get the document ID of a specific document in Firebase on React Native? 使用字段值删除 firebase 文档 - Delete firebase document using field value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM