为什么FireStore在哪里查询不起作用?

时间:2018-08-30 20:11:40

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

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' }

这是我的数据库: enter image description here

2 个答案:

答案 0 :(得分:5)

问题是您正在使用

.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
     }
    })
  })

答案 1 :(得分:3)

当您对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);
    });