首页 > 解决方案 > 如何使用nodejs从云firestore中的单个查询中获取响应对象?

问题描述

我正在尝试在 cloud firestore 中查询并尝试提取文档 id,还有另一个值也称为 cc 我能够提取信息但使用 .forEach 但我想问是否有另一种方法可以做到这一点是我的询问

const recipient = await db
    .collection("Active")
    .where("office", "==", "d")
    .where("template", "==", "dt")
    .where("attachment.Name.value", "==", "xyz")
    .get();

const id = [];
  const cc = [];
  recipient.forEach((doc) => {
    id.push(doc.id);
    cc.push(doc.data().cc);
    // console.log(JSON.stringify(doc.data()));
  });

标签: javascriptnode.jsgoogle-cloud-firestore

解决方案


如果查询结果中只有一个文档,

const recipient = await db
    .collection("Active")
    .where("office", "==", "d")
    .where("template", "==", "dt")
    .where("attachment.Name.value", "==", "xyz")
    .get();

const id = [];
const cc = [];

id.push(recipient.docs[0].id);
cc.push(recipient.docs[0].data().cc);


推荐阅读