Nick
Nick

Reputation: 195

I am trying to query timestamps in firestore

Here is my code. Right now, I am just trying to make sure I know how to format my date to get it. I have tried Month-date-year and that didn't seem to work. I was able to get the object that firestore passed it looked like this.

{ Cust_Notes: "Quisque malesuada sagittis posuere. Vestibulum leo enim, aliquam ut fermentum id, vestibulum eu lacus. Maecenas ornare ultrices dui nec facilisis. Vivamus convallis eros at
Date_of_Appt: Timestamp nanoseconds: 0 seconds: 1577336400 }

       firebase
      .firestore()
      .collection("appointments")
      .where("UserId", "==", user.uid)
      .where("Date_of_Appt", "==", "1577336400")
      .get()
      .then(function(querySnapshot) {
        querySnapshot.forEach(function(doc) {
          // doc.data() is never undefined for query doc snapshots
          console.log(doc.id, " => ", doc.data());
        });
      })
      .catch(function(error) {
        console.log("Error getting documents: ", error);
      });
    this.setState({
      uid: user.uid
    });
  };

Upvotes: 0

Views: 216

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599946

You're passing in a string value for Date_of_Appt right now. Since you're storing Date_of_Appt as Timestamp objects in the database, comparing it to a string will never match a document.

The solution to pass in a JavaScript Date object, or a Firestore Timestamp object. Since you seem to have the time as an interval in seconds, the easiest is a timestamp like this:

firebase
  .firestore()
  .collection("appointments")
  .where("UserId", "==", user.uid)
  .where("Date_of_Appt", "==", new firebase.firestore.Timestamp(1577336400,0))
  .get()

The above will only return documents that exactly match the timestamp value that you pass in. If you want to return documents with a Date_of_Appt within a range, you'll want to query with >= and <=.

Upvotes: 1

Related Questions