【问题标题】:await reserved word problem at firebase v9在firebase v9等待保留字问题
【发布时间】:2021-11-15 09:50:30
【问题描述】:

如果我使用 firebase 文档中的代码,则会收到错误 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);
}

如果我这样使用它,我不会收到任何错误,但这不起作用

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)
  }
}

谁能帮我做错什么。

【问题讨论】:

  • 究竟是什么不起作用?应该发生什么?会发生什么?
  • 这个函数永远不会触发。我猜它应该并重新调整错误或将任何内容记录到控制台
  • 看来您使用的是await 却没有在异步函数中,但是您创建了一个从未调用过的异步函数?如果这是正确的,请务必在第二个示例中调用该函数。

标签: javascript firebase nuxt.js


【解决方案1】:

正如 Brian 所说,在第一个示例中,您在一个非异步函数中调用 await,而在第二次尝试中,您只定义了一个从未调用过的函数。

我的建议:创建一个异步函数,并在需要的地方调用它,使用 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
    });
    ...

【讨论】:

  • 我想通了并修复了它,但你的方法似乎更好我现在试试看
  • 好的 :)。请您支持我的回答吗?
猜你喜欢
  • 1970-01-01
  • 2018-02-03
  • 2022-12-15
  • 2020-05-14
  • 1970-01-01
  • 2021-11-05
  • 2023-03-12
  • 2018-12-25
  • 2018-05-28
相关资源
最近更新 更多