且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Firestore中使用get时如何等待诺言

更新时间:2023-01-18 14:07:54

您有2个选择:您可以使用 async / await 或根据您希望代码的执行方式使用 Promise.then()

You have 2 options: you can use async/await or you can use Promise.then() depending on how you want the code to execute.

异步/等待:

async function databasetest {
  var cityRef;
  try{
    cityRef = await db.collection('cities').doc('SF');
    // do stuff
  } catch(e) {
    // error!
  }

Promise.then():

Promise.then():

db.collection('cities').doc('SF').then((cityRef) => {
  cityRef.get()
    .then(doc => { /* do stuff */ })
    .catch(err => { /* error! */ });
});