wilu
wilu

Reputation: 559

Reading from Firestore returns nothing

I'm trying to read/write from function to Firestore and have a problem with reads, writes work fine. I was following examples presented here but the query below (and its variants) returns empty promise:

module.exports.customerByPhone = phone => {
    return db.collection('customers')
    .limit(1)
    .get();
    // .where('phoneNumber', '==', phone).get();
};

As u see, initially I was trying with where - empty promise, I also tried with whatever - same result.

Here's how I insert data:

db.collection('customers').add({
        phoneNumber: 123456,
        active: true,
        registration: new Date()
    }).then(it => console.log(`added ${it}`));

Package.json fragment:

"dependencies": {
    "firebase-admin": "^8.6.0",
    "firebase-functions": "^3.3.0"
  }

And some auth logic:

const key = require("../firebase.key.json");
//
admin.initializeApp({
    credential: admin.credential.cert(key),
    databaseURL: "https://cant-touch-this.firebaseio.com"
});
const db = admin.firestore();

EDIT

Here's more complete app flow,

Request handler looks like:

module.exports.handle = (req, res) => {
    // doInsert and query below are just for test purposes
    db.doInsert();
    db.customerByPhone(req.body.phoneNumber).then(function (doc) {
        if (doc.exists) {
            console.log("Document data:", doc.data());
        } else {
            // doc.data() will be undefined in this case
            console.log("No such document!");
        }
    });
    ...
}

As I said, doInsert works, implementation:

module.exports.doInsert = () => {
    db.collection('customers').add({
        phoneNumber: 123456,
        active: true,
        registration: new Date()
    }).then(it => console.log(`added ${it}`));
}

Now the querying part, it juts returns one element, impl:

module.exports.customerByPhone = phone => {
    return db.collection('customers')
        // .where('phoneNumber', '==', phone)
        .limit(1)
        .get()
};

Finally a pic from the db, not in EN but crystal clean: enter image description here

What do I miss??

Upvotes: 0

Views: 130

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317352

You are storing the phone number as an integer:

db.collection('customers').add({
    phoneNumber: 123456,  // THIS IS AN INTEGER
    active: true,
    registration: new Date()
})

But you are querying it with a string:

// req.body.phoneNumber is a string
db.customerByPhone(req.body.phoneNumber)

In Firestore, strings and numbers are never equal to each other, so this query would never work.

Since phone numbers are not really integers. They don't represent a numeric value, and you would never do match with them. So you should store them as a string instead:

db.collection('customers').add({
    phoneNumber: '123456',  // THIS IS A STRING
    active: true,
    registration: new Date()
})

Put quotes around the number when you call add() so Firebase knows you want to store a string. Then your query with the same string will find the document.

Upvotes: 1

Related Questions