How to throw an error if firestore document does not exist?
const cancel = (id) => {
if(window.confirm("Are you sure to cancel this appointment ?")) {
const userDoc = doc(db, 'accounts', id)
deleteDoc(userDoc)
}
}
How to throw an error if firestore document does not exist?
const cancel = (id) => {
if(window.confirm("Are you sure to cancel this appointment ?")) {
const userDoc = doc(db, 'accounts', id)
deleteDoc(userDoc)
}
}
2
Answers
you can use the condition
if(!userDoc.exist) return ;
·it doesn’t work for because you need to use it this way
Your
userDoc
variable is a reference to a document, it doesn’t yet have any data of that document from the database.If you want to check whether the document exists, you’ll first have to get it from the database. From that link comes this code sample, which is quite close to what you’re trying to do: