If I use the code as in firebase documentation I get the error await is reserved word
try {
const docRef = await addDoc(collection(db, "users"), {
first: "Alan",
middle: "Mathison",
last: "Turing",
born: 1912
});
console.log("Document written with ID: ", docRef.id)
} catch (e) {
console.error("Error adding document: ", e);
}
If I use it like this I don't get any error but this doesn't work
async () => {
try {
const docRef = await addDoc(collection(db, "projects", vm.add.slug), {
first: "Alan",
middle: "Mathison",
last: "Turing",
born: 1912
})
console.log("Document written with ID: ", docRef.id)
} catch (err) {
console.log("error deleting data:", err)
}
}
Can anyone help me what do I do wrong.
As Brian said, in the first example, you're calling await in a function that is not an async one, and in the second try, you've only defined a function that was never called.
My suggestion: Create an async function, and call it where you want, with await.
const submit = async payload => {
try {
const docRef = await addDoc(collection(db, "projects", vm.add.slug), payload)
console.log("Document written with ID: ", docRef.id)
} catch (err) {
console.log("error deleting data:", err)
}
};
...
await submit({
first: "Alan",
middle: "Mathison",
last: "Turing",
born: 1912
});
...