简体   繁体   English

未捕获的语法错误:意外的保留字(等待)

[英]Uncaught SyntaxError: Unexpected reserved word (await)

guys.伙计们。 I'm using Google Firestore to save some data in a database.我正在使用 Google Firestore 将一些数据保存在数据库中。 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 .例如,如果我使用 Google 提供的代码在数据库中替换或创建新文档,则会收到错误消息: 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.我知道问题是 await 只能与异步函数一起使用,但这些不是我创建的函数。 They are imported from the Firestore libraries and I'm using the code provided by Google.它们是从 Firestore 库中导入的,我使用的是 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"
})

The "await" only makes the rest of the code wait for the method to finish. “await”只会让其余的代码等待方法完成。
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"
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM