Home > Software design >  React Native Firestore document deletion
React Native Firestore document deletion

Time:05-24

I'm trying to run the code below, but it doesn't work. Can you help me?

await followRequestsColl.doc()
        .where('Followed', '==', user.uid)
        .where('Following', '==', targetUserUid)
        .delete().then(() => {
          console.log('User deleted!');
        });

Error : Possible Unhandled Promise Rejection (id: 6): TypeError: followRequestsColl.doc().where is not a function. (In 'followRequestsColl.doc().where('Followed', '==', user.uid)', 'followRequestsColl.doc().where' is undefined)

CodePudding user response:

You should only search in collections:

await followRequestsColl.collection('somecollectionname')
    .where('Followed', '==', user.uid)
    .where('Following', '==', targetUserUid)
    .delete().then(() => {
      console.log('User deleted!');
    });

CodePudding user response:

In addition to RobrechtVMs observation that you can't query a document. you also can't call delete() on a query.

You will have to:

  1. Execute the query
  2. Loop over the results
  3. Delete each document in turn

Something like:

const query = followRequestsColl.collection('somecollectionname')
  .where('Followed', '==', user.uid)
  .where('Following', '==', targetUserUid);
query.get().then((snapshot) => {
  snapshot.docs.forEach((doc) => {
    doc.ref.delete();
  })
})

Also see: How to delete document from firestore using where clause

  • Related