Rayinator
Rayinator

Reputation: 45

Firestore query returning empty snapshot

I have a node websocket listener set to query some data from my database. Currently however the query returns an empty snapshot even though I know that the query should return something.

I'm making a simple node based forum (although I'm pretty new to Node so it's not that good lol) and I'm trying to query post data from the server to send back to the client. I've tried checking to see if my post references were correct and double checked that the values I'm querying exist within the collection. I've also already checked that the data was sent through the socket correctly so I'm at a loss.


let query = db.collection('posts').where('id','==',postID);
query.get().then(snapshot => {
      if (snapshot.empty) {
        client.emit('resPost', 'Failed');
        return;
      }
      snapshot.forEach(doc => {
        console.log(doc.data());
        client.emit('resPost', doc.data());
      });
});

When I load the post page I get the response tied to the empty snapshot.

Edit: As suggested by @Phil I've included a log for the postID variable.

console.log('postID', typeof postID, JSON.stringify(postID))

Output:

postID string "0"

Here's a screenshot of the console if needed. The ClientID is the ID of the websocket connection. https://i.sstatic.net/9AwLX.jpg

Upvotes: 3

Views: 3704

Answers (1)

Phil
Phil

Reputation: 164736

I'm quite surprised there isn't a duplicate question anywhere but here's your answer...

Firestore's query equality conditionals are type-sensitive. If your collection property is an integer, you need to compare it to an integer instead of a string.

For example

db.collection('posts').where('id', '==', parseInt(postID, 10))

Upvotes: 3

Related Questions