【问题标题】:Can I use async await with firebase database call?我可以将 async await 与 firebase 数据库调用一起使用吗?
【发布时间】:2017-11-20 01:09:33
【问题描述】:

我想使用 async await 因为我发现它比 then 方法方便得多。但是当我这样做时,我得到了这个错误:

const onceGetUsers = async () => await db.ref("users").once("value");
let users = await db.onceGetUsers(); // Syntax error: await is a reserved word
console.log(users);

谢谢

【问题讨论】:

  • 您必须将其放在async function 中。你不能随便在任何地方使用它。
  • 顺便说一句,onceGetUsers 中的awaitpretty useless

标签: javascript firebase firebase-realtime-database async-await


【解决方案1】:

您似乎没有正确使用awaitawait 只能使用 inside a function declared async。在您的代码中,您在外部使用了 await(除非您的代码中还有其他内容没有显示)。

【讨论】:

  • 你说得对,我的顶级函数中缺少异步。谢谢!
猜你喜欢
  • 1970-01-01
  • 2022-01-03
  • 2018-10-17
  • 2022-11-23
  • 2017-11-05
  • 2019-04-15
  • 2014-06-19
  • 2018-01-14
  • 2018-09-12
相关资源
最近更新 更多