简体   繁体   English

未捕获的 SyntaxError:await 仅在异步函数中有效(FireBase 文档)

[英]Uncaught SyntaxError: await is only valid in async function (FireBase documentation)

I'm trying to get data from my Firestore database.我正在尝试从 Firestore 数据库中获取数据。 Based on Firestore documentation I wrote this:基于Firestore 文档,我写了这个:

const productsRef = db.collection('products');
const snapshot = await productsRef.get();
snapshot.forEach(doc => {
  console.log(doc.id, '=>', doc.data());
});

I get the error in the title.我收到标题中的错误。 Why do they share a code that doesn't work or am I missed something?为什么他们共享不起作用的代码或者我错过了什么? Thank you!谢谢!

You need a async function in order to use the await keyword.您需要一个async函数才能使用await关键字。
If you don't have a function in wich this code is ran you can use an IIFE wrapper如果您没有运行此代码的函数,您可以使用 IIFE 包装器

(async ()=>{
  const productsRef = db.collection('products');
  const snapshot = await productsRef.get();
  snapshot.forEach(doc => {
    console.log(doc.id, '=>', doc.data());
  });
})();

or don't use await at all and handle it into the then chain :或者根本不使用 await 并将其处理到then链中:

const productsRef = db.collection('products');
const snapshot = productsRef.get().then((snapshot)=>{
  snapshot.forEach(doc => {
    console.log(doc.id, '=>', doc.data());
  });
});

The await keyword is used for consuming promises, along with async . await关键字与async一起用于消费承诺。 But in order for await to work you must wrap it inside a function declared with the async keyword, like so:但是为了让 await 工作,您必须将它包装在一个用async关键字声明的函数中,如下所示:

const getProducts = async () => {
  const productsRef = db.collection('products');
  const snapshot = await productsRef.get();
  snapshot.forEach(doc => {
    console.log(doc.id, '=>', doc.data());
  });
}

getProducts();

暂无
暂无

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

相关问题 未捕获的SyntaxError:await仅在异步函数的异步函数中有效 - Uncaught SyntaxError: await is only valid in async function in async function 未捕获的 SyntaxError:await 仅在异步函数中有效(尽管它在异步函数中......) - Uncaught SyntaxError: await is only valid in async function (its in an async function though…) javascript await function in script 返回 Uncaught SyntaxError: await is only valid in async functions and the top level body of modules - javascript await function in script returns Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules Javascript:SyntaxError:await仅在异步函数中有效 - Javascript: SyntaxError: await is only valid in async function meteo.js:21 Uncaught SyntaxError:await仅在异步函数中有效 - meteo.js:21 Uncaught SyntaxError: await is only valid in async function SyntaxError: await 仅在异步中有效 function **而且我有异步** - SyntaxError: await is only valid in async function **and I have async** SyntaxError: await 仅在 async 函数中有效。 无法纠正 - SyntaxError: await is only valid in async function. Unable to Correct it Appium 代码生成“SyntaxError: await is only valid in async function” - Appium code generates "SyntaxError: await is only valid in async function" Node Js - SyntaxError: await 仅在异步中有效 function - Node Js - SyntaxError: await is only valid in async function SyntaxError: await 仅在我的代码中的异步 function 中有效 - SyntaxError: await is only valid in async function in my code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM