0

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
    });
  };
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Nick
  • 195
  • 1
  • 3
  • 13
  • Dates in Firestore are stored as `Timestamp` objects. To get a JavaScript `Date` object, call `toDate()` on the `Timestamp` object. See https://stackoverflow.com/questions/52247445/how-do-i-convert-a-firestore-date-timestamp-to-a-js-date – Frank van Puffelen Dec 14 '19 at 02:12
  • I am not trying to get convert it right now. I just to query the timestamps first. – Nick Dec 14 '19 at 02:22
  • @Frank van Puffelen, would have to import that method into javascript? – Nick Dec 14 '19 at 02:37
  • Ah, got it. That's the other way around indeed. `new firebase.firestore.Timestamp(1577336400,0)` – Frank van Puffelen Dec 14 '19 at 02:45

1 Answers1

1

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 <=.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807