【问题标题】:Why is FireStore where query not working?为什么查询不起作用的 FireStore?
【发布时间】:2019-02-05 20:25:12
【问题描述】:

FireStore where 不工作或我做错了什么。我正在尝试通过电子邮件用户:tolotra@tolotra.com

代码如下:

const admin = require('firebase-admin');
var serviceAccount = require('../secret/nicecode-e3e53-2ddaa9d588ea.json');
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});
var db = admin.firestore();
const settings = {/* your settings... */ timestampsInSnapshots: true};
db.settings(settings);



function begin(email){
    return new Promise(function (res,rej){
        var ans

        var citiesRef = db.collection('users');
        var allCities = citiesRef.get()
            .then(snapshot => {
                snapshot.forEach(doc => {
                    console.log(doc.id, '=>', doc.data());
                });
            })
            .catch(err => {
                console.log('Error getting documents', err);
            });

        var citiesRef = db.collection('users');
        console.log(email)
        var query = citiesRef.where('email', '==', email).get()
            .then(doc => {
                if (!doc.exists) {
                    ans = {
                        "login_token": "7705cb58bbff3afa888a624747f8d1caaff08f6ed0dc465c7d2361fd62532bd7",
                        "next": "FAILURE",
                        "failure_step": {
                            "heading": "Error",
                            "description": "There is no account registered for "+email,
                            "actions": [{"label": "Sign up", "url": "/en/signup"}, {
                                "label": "Sign in",
                                "url": "/en/login"
                            }]
                        }
                    }

                } else {
                    console.log('Document data:', doc.data());
                    ans = {
                        "login_token": "cd01b73935b9262ac8a2bac3f81b6ed6ec78c15d3da5fc04f03879ebdd6931a9",
                        "next": "PASSWORD",
                        "password_step": {"heading": "Password", "description": "Enter your password", "error": ""}
                    }
                }
                res(ans)
            })
            .catch(err => {
                console.log('Error getting document', err);
            });
    })
}

begin('tolotra@tolotra.com').then(function (data) {
console.log(data)
})

输出这个。 (请注意,如果我只是获取所有用户,则会找到电子邮件为 'tolotra@tolotra.com' 的用户)

tolotra@tolotra.com
{ login_token: '7705cb58bbff3afa888a624747f8d1caaff08f6ed0dc465c7d2361fd62532bd7',
  next: 'FAILURE',
  failure_step: 
   { heading: 'Error',
     description: 'There is no account registered for tolotra@tolotra.com',
     actions: [ [Object], [Object] ] } }
OqClQYBH8PWiNE2VF193 => { email: 'tolotra@tolotra.com',
  password_hash: 'E014231EF9830992D2E367231EEDE99C189B6518CE70E2AB8F414C784257751F' }

这是我的数据库:

【问题讨论】:

    标签: node.js firebase google-cloud-firestore


    【解决方案1】:

    问题是你正在使用

    .where('email', '==', email).get()

    这意味着它将返回一个数组。

    但是,您正在使用

    !doc.exists

    这在

    上不存在

    .where

    你需要使用

    const query = firebase
      .firestore()
      .collection("users")
      .where("email", "==", email).get()
      .then(snapshot => {
    
        if (snapshot.empty) {
        // No Results
        }
    
        snapshot.docs.forEach(document => {
         if (document.exists) {
           // Do Something
         } else {
           // Do Something Else
         }
        })
      })
    

    【讨论】:

      【解决方案2】:

      当您对 Firestore 运行查询时,可能有多个文档符合您的条件。这就是为什么你得到的结果不是Document,而是QuerySnapshot。并且(与 Document 不同)QuerySnapshot 没有 exists() 方法,但它有一个您可以使用的 empty 属性。

      所以:

      var query = citiesRef.where('email', '==', email).get()
          .then(querySnapshot => {
              if (querySnapshot.empty) {
                  ans = {
                      "login_token": "7705cb58bbff3afa888a624747f8d1caaff08f6ed0dc465c7d2361fd62532bd7",
                      "next": "FAILURE",
                      "failure_step": {
                          "heading": "Error",
                          "description": "There is no account registered for "+email,
                          "actions": [{"label": "Sign up", "url": "/en/signup"}, {
                              "label": "Sign in",
                              "url": "/en/login"
                          }]
                      }
                  }
      
              } else {
                  var doc = querySnapshot.docs[0];
                  console.log('Document data:', doc.data());
                  ans = {
                      "login_token": "cd01b73935b9262ac8a2bac3f81b6ed6ec78c15d3da5fc04f03879ebdd6931a9",
                      "next": "PASSWORD",
                      "password_step": {"heading": "Password", "description": "Enter your password", "error": ""}
                  }
              }
              res(ans)
          })
          .catch(err => {
              console.log('Error getting document', err);
          });
      

      【讨论】:

      • 获取文档时出错 TypeError:querySnapshot.docs 不是 cityRef.where.get.then.querySnapshot 的函数(C:\Users\Tolotra Samuel\PhpstormProjects\CryptOcean\controller\controller.js:39 :49) 在 process._tickCallback (internal/process/next_tick.js:109:7)
      • 感谢您修复 TSR!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 2016-07-30
      • 2017-08-02
      • 2013-04-14
      相关资源
      最近更新 更多