小编Rob*_*ard的帖子

从Firestore获取异步值

我正在努力进行异步操作。我试图简单地从firestore中获取值并将其存储在var中。

我设法接收到该值,当我专门这样做时(甚至可以在get函数中使用var),甚至可以将其保存在var中,但是当尝试以灵活的方式保存它时,我似乎无法正确地管理await:

async function getValues(collectionName, docName,) {
console.log("start")
var result;
var docRef = await db.collection(collectionName).doc(docName).get()
  .then(//async// (tried this as well with async) function (doc) {
    if (doc.exists) {
      console.log("Document data:", doc.data());
      result = doc.data().text;
      console.log(result);
      return //await// (this as well with async) result;
    } else {
      // doc.data() will be undefined in this case
      console.log("No such document!");
      result = "No such document!";
      return result;
    }
    console.log("end");
  }).catch (function (err) {
    console.log('Error getting documents', err);
  });
};

helpMessage = getValues('configuration','helpMessage');
Run Code Online (Sandbox Code Playgroud)

注意: …

asynchronous function node.js google-cloud-firestore

3
推荐指数
1
解决办法
854
查看次数