【问题标题】:Uncaught SyntaxError: Unexpected reserved word (await)未捕获的 SyntaxError:意外的保留字(等待)
【发布时间】:2021-12-08 01:36:13
【问题描述】:

伙计们。我正在使用 Google Firestore 将一些数据保存在数据库中。例如,如果我使用 Google 提供的代码在数据库中替换或创建新文档,我会收到错误:Uncaught SyntaxError: Unexpected reserved word。我知道问题是 await 只能与异步函数一起使用,但这些不是我创建的函数。它们是从 Firestore 库中导入的,我使用的是 Google 提供的代码。这里有什么问题?谢谢。

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

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

【问题讨论】:

    标签: javascript google-cloud-firestore async-await


    【解决方案1】:

    “等待”只会让其余代码等待方法完成。
    如果你想保留它:

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

    如果没有:

    import { doc, setDoc } from "firebase/firestore";
    
    setDoc(doc(db, "cities", "LA"), {
      name: "Los Angeles",
      state: "CA",
      country: "USA"
    });
    

    【讨论】:

    • 我明白你的意思,但这很令人困惑,因为代码是由谷歌作为示例提供的,不仅使用 setDoc 方法,而且还有许多其他方法。如果您的观点是正确的,那么 Google 为什么要使用这样的代码?见这里:firebase.google.com/docs/firestore/query-data/get-data?hl=en
    • 在谷歌的情况下,它需要等待来保证函数一个接一个地被调用
    猜你喜欢
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    相关资源
    最近更新 更多