yaakov
yaakov

Reputation: 4665

firestore timestamp query not returning value

I'm trying to query a list of games from Firestore that are in the future.

const gamesRef = db.collection("games");
gamesRef
  .where("kickoff", ">", 0)
  .get()
  .then((snap) => {
    console.log(snap);
  });

db is the firebase.firestore(), and if I call it without the query it returns the data correctly.

enter image description here

I'm not sure what is wrong here. This is the output from the request:

enter image description here

Upvotes: 0

Views: 63

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

Firstly, you must be mistaken about db being firebase.database(). It should be firebase.firestore(). They are different APIs for different database systems. What you're showing here is Firestore, not Realtime Database.

Secondly, snap is a QuerySnapshot type object. Your code is just printing the JavaScript default string representation of a QuerySnapshot object. That's not going to be useful to you at all. You will need to use the QuerySnapshot API to get the values of the documents that resulted from the query, as illustrated in the documentation:

  .then((snap) => {
    snap.forEach(function(doc) {
      // doc.data() is never undefined for query doc snapshots
      console.log(doc.id, " => ", doc.data());
    });
  });

And thirdly, I can see from your output that there are no documents in the result set, (docs: Array[0]) so your code won't print anything at all. Your query is trying to compare a timestamp type field to a integer. These are not comparable field values. If you have a timestamp field, you're going to have to compare it to another timestamp type value. You can use JavaScript objects or Firestore Timestamp objects:

const gamesRef = db.collection("games");
gamesRef
  .where("kickoff", ">", new Date())

Upvotes: 1

Related Questions