I've been trying to figure out how to handle Firebase errors for the firestore using async/await. I want to show an error message, if a doc doesn't exist but I'm a little confused about how I would do that with async/await.
getResult.js
If I purposely give a fake document ID, the catch function automatically takes over and I can no longer access result.exists which is what I need in this case. Is there a way I can easily check what type of error it is when I catch it, so I can return an appropriate message?
export const getResult = async (collection, doc) => {
try {
const resultRef = db.collection(collection);
const result = await resultRef.doc(doc).get();
if (result.exists) {
return { data: result };
} else {
return { data: "Error retrieving document, as it doesnt exist" };
}
} catch (err) {
if (err.response) {
console.log(err.response.data);
return err.response.data;
} else if (err.request) {
console.log(err.request);
return err.request;
} else {
console.log(err.message);
return err.message;
}
}
};