I have a async function like this:
exports.myFunction = async (req, res, next) => {
if (some condition) {
next()
}
try {
const results = await axios.get(`https://a-domain.com/url/path`);
const info = results.data;
const docRef = await addDoc(collection(db, "name_of_collec"), info);
console.log("Document written with ID: ", docRef.id);
} catch (e) {
console.error("Error adding document: ", e);
}
next();
};
Here, response from the first request is required in the second request. But according to my knowledge since I am making asynchronous requests, the two requests should be made at the same time.
What I cannot understand:
- So, how it is possible that there is no error occurring?
- How is it that, sometimes the no document is added to Firestore? Like it is skipped somehow…
Context info:
- I am using Express.js and Node.js to make a backend
- I am using axios to make the first request and the second one is adding a document to Firestore.
- Firestore is a noSQL database inside Google’s Firebase.