繁体   English   中英

Firestore,将特定字段检索为 JSON?

[英]Firestore, retrieve specific fields as JSON?

我正在尝试从给定的 Firestore 集合(在 HTTPSCallable 内部)中检索特定字段,但是对于我知道存在且有数据的字段,我得到了“空值”。 任何想法我做错了什么?

var docs;
        await db
          .collection(mycollection)
          .where("flag", "==", "true")
          .get()
          .then((querySnapshot) => {
            docs = querySnapshot.docs.map((doc) => {
              doc.data();
            });
          });
        return JSON.stringify({ mydocs: docs });

您应该避免同时使用 promise 链接和 async-await。 return 语句将在不检查 Firestore 返回的承诺是否已解决的情况下执行。 试试这个:

const querySnapshot = await db
          .collection(mycollection)
          .where("flag", "==", "true")
          .get()

const docs = querySnapshot.docs.map((doc) => doc.data());

return JSON.stringify({ mydocs: docs });

如果你正在使用 promise 链,那么在 promise 被解决后应该发生的任何事情最好都应该在then()块内:

const myQuery = db.collection(mycollection).where("flag", "==", "true")
return myQuery.get().then((querySnapshot) => {
  const docs = querySnapshot.docs.map((doc) => doc.data());

  return JSON.stringify({ mydocs: docs });
})

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM