简体   繁体   中英

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. 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

What am I doing wrong?

Since you're looping over a QuerySnapshot each doc object is a QueryDocumentSnapshot , while deleteDoc expects a DocumentReference . To get from the former to the latter, you can call .ref on the snapshot.

So:

querySnapshot.forEach(doc => {
  deleteDoc(doc.ref)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM