代码之家  ›  专栏  ›  技术社区  ›  Thomas David Kehoe

Firebase Firestore where()查询找不到文档

  •  0
  • Thomas David Kehoe  · 技术社区  · 7 年前

    where().get() 对于Firestore查询,我无法让它工作。我的Firestore数据:

    enter image description here

    此代码适用于:

    firebase.firestore().collection('Tunes').doc('tiny_dancer')
    .get()
    .then(function(querySnapshot) {
      if (querySnapshot.exists) {
        console.log(querySnapshot.data());
      } else {
        console.log("No such document!");
      }
    })
    .catch(function(error) {
       console.error(error);
    });
    

    我们回来了 {composer: "elton_john"} .

    此代码不起作用:

    firebase.firestore().collection('Tunes')
    .where('composer', '==', 'elton_john')
    .get()
    .then(function(querySnapshot) {
      if (querySnapshot.exists) {
        console.log(querySnapshot.data());
      } else {
        console.log("No such document!");
      }
    })
    .catch(function(error) {
       console.error(error);
    });
    

    答案是 No such document! 我期待它回来 tiny_dancer . 我该怎么做?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Frank van Puffelen    7 年前

    有一个 DocumentSnapshot.exists 属性,以允许您检查文档是否实际存在。但是对于查询,可以有任意数量的匹配文档,因此 exists QuerySnapshot.empty 检查是否有任何匹配项。