Home > Software engineering >  Delete document with async/await in Firestore V9
Delete document with async/await in Firestore V9

Time:04-01

I'm using Firestore v9 within Expo/React Native. I can't properly delete a document using async/await and deleteDoc function. The document removes fine but my app doesn't wait for the completed operation as I expect. It behaves asynchronously.

This is the call code

showDeleteConfirmation(data)
{
    Alert.alert(
        this.title,
        this.message,
        [
          {text: "Yes", onPress: () => this.deleteDococument(data)},
          {text: "No", style: 'cancel'},
        ],
        { cancelable: false }
    )
}

async deleteDococument(data)
{
    const isDataDeleted = await deleteDocFirestore(data)
    console.log(isDataDeleted) // i get undefined
}

deleteDocFirestore function

const deleteDocFirestore = async (user) => {
    try {

    const docUserRef = doc(db, "users", user.UserId);
    
    const userDeleted = await deleteDoc(docUserRef);

    return userDeleted
  } catch (error) {
    console.log(`error`, error.message);
  }
};

CodePudding user response:

The deleteDoc() function returns void so you can simply return true from deleteDocFirestore function or false if an error is thrown.

  • Related