我正在尝试查询Firestore中的时间戳

时间:2019-12-14 01:54:56

标签: javascript firebase google-cloud-firestore

这是我的代码。现在,我只是想确保我知道如何格式化日期以获取日期。我已经尝试了Month-date-year,但似乎没有用。我能够获得Firestore通过它的对象,如下所示。

{  客户注意事项:“ Quisque malesuada sagittisposuere。Vestibulumleo enim,aliquam ut fermentum id,              欧洲前庭。 Maecenas ornare ultrices dui ne facilisis。 Vivamus convallis eros at
应用日期:时间戳              纳秒:0            秒: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
    });
  };

1 个答案:

答案 0 :(得分:1)

您现在正在传递Date_of_Appt的字符串值。由于您将Date_of_Appt作为Timestamp对象存储在数据库中,因此将其与字符串进行比较将永远不会与文档匹配。

传递JavaScript Date对象或Firestore Timestamp对象的解决方案。由于您似乎将时间间隔定为秒,因此最简单的是这样的时间戳:

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

以上内容仅返回与您传递的时间戳值完全匹配的文档。如果您要返回范围内带有Date_of_Appt的文档,则需要使用>=<=