Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is FireStore where query not working?

FireStore where is not working or I am doing something wrong. I am trying to user by email: [email protected]

Here is the code:

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('[email protected]').then(function (data) {
console.log(data)
})

Outputs this. (Notice that the user with email '[email protected]' is found if I just fetch all users)

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

Here is my database: enter image description here

like image 450
TSR Avatar asked Aug 30 '18 20:08

TSR


People also ask

How does Firestore query work?

Cloud Firestore caches data that your app is actively using, so the app can write, read, listen to, and query data even if the device is offline. When the device comes back online, Cloud Firestore synchronizes any local changes back to Cloud Firestore.

How do I find my Firestore collection path?

Hover your mouse over the path area until you see the pencil. Click the pencil. Copy the entire path (without the first, left-most forward slash) and paste it into the Authorization form in Flatly.

How many reads can Firestore handle?

10 for single-document requests and query requests. 20 for multi-document reads, transactions, and batched writes. The previous limit of 10 also applies to each operation.


1 Answers

When you run a query against Firestore there may be multiple documents that match your conditions. That's why the result you get is not a Document, but a QuerySnapshot. And (unlike Document) QuerySnapshot does not have an exists() method, but it does have an empty property that you can use.

So:

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);
    });
like image 54
Frank van Puffelen Avatar answered Nov 12 '22 22:11

Frank van Puffelen