【问题标题】:Firebase firestore query not returning dataFirebase Firestore 查询不返回数据
【发布时间】:2020-06-26 03:28:00
【问题描述】:

我正在我的 firestore 数据库中查询工作区 ID。但是,snapshot.empty 评估为 true,即使我可以在我的 firestore 仪表板中看到具有正确 ID 的文档。我是否错误地查询数据库?结果是console.log("No matching documents")

TIA

async function my_func(workspaceID) {
    console.log(workspaceID);
    db.collection('workspaces')
      .where('team_id', '==', workspaceID)
      .get()
      .then((snapshot) => {
        console.log(snapshot);
        if (snapshot.empty) {
            console.log('No matching documents.');
            return;
        }  
      })
      .catch((err) => console.log("Error fetching doc for wsID, ", err))
};

编辑

本例中team_id的值为“T015KLJHGV9”,是一个字符串。 workspaceID 的值,当我用typeof workspaceID 检查它时也是一个字符串

【问题讨论】:

  • Firestore 的查询相等条件是类型敏感的,所以试试这个.where('team_id', '==', parseInt(workspaceID, 10))
  • 请编辑问题以显示您尝试查询的数据。如果我们看不到您的查询实际上与您的数据匹配,我们无能为力。我们需要知道您正在使用的 workspaceID 的值,以及应该匹配的特定文档。

标签: javascript firebase google-cloud-firestore


【解决方案1】:

你可以试试:

async function my_func(workspaceID) {
    console.log(workspaceID);
    db.collection('workspaces')
      .where('newWorkspace.team_id', '==', workspaceID)
      .get()
      .then((snapshot) => {
        console.log(snapshot);
        if (!snapshot.exists) {
            console.log('No matching documents.');
            return;
        }  
      })
      .catch((err) => console.log("Error fetching doc for wsID, ", err))
};

【讨论】:

  • 谢谢 - 我试过了,结果一样。 snapshot.exists 计算结果为 undefined,这就是 if 语句仍然被触发的原因
  • snapshot.forEach((doc) => { console.log(doc.data()); })
  • 在这种情况下没有打印任何内容 - 可能是因为它是一个空数组?
  • 你的条件必须是:.where('newWorkspace.team_id', '==', workspaceID)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-21
  • 1970-01-01
相关资源
最近更新 更多