Uncaught SyntaxError: Unexpected reserved word (await)

Viewed 519

guys. I'm using Google Firestore to save some data in a database. If, for example, I use the code provided by Google to replace or create a new document in the database, I get the error: Uncaught SyntaxError: Unexpected reserved word. I know the problem is that await can only be used with asynchronous functions, but these are not functions created by me. They are imported from the Firestore libraries and I'm using the code provided by Google. What is wrong here? Thank you.

import { doc, setDoc } from "firebase/firestore";

await setDoc(doc(db, "cities", "LA"), {
  name: "Los Angeles",
  state: "CA",
  country: "USA"
})
1 Answers

The "await" only makes the rest of the code wait for the method to finish.
If you want to keep it:

import { doc, setDoc } from "firebase/firestore";

(async function() {
  await setDoc(doc(db, "cities", "LA"), {
    name: "Los Angeles",
    state: "CA",
    country: "USA"
  })
})();

If not:

import { doc, setDoc } from "firebase/firestore";

setDoc(doc(db, "cities", "LA"), {
  name: "Los Angeles",
  state: "CA",
  country: "USA"
});
Related