Javascript Firestore orderBy时间戳对象

Javascript Firestore orderBy时间戳对象,javascript,reactjs,firebase,react-native,google-cloud-firestore,Javascript,Reactjs,Firebase,React Native,Google Cloud Firestore,我正在尝试按时间戳排序查询 在我的文档中,我有一个名为“日期”的字段,其形式如下: date = { nanoseconds: 963000000, seconds: 1594917688 } 在我的代码中,我有: let photosArray = []; firebase .getDatabase() .collection("photos") .doc(firebase.getCurrentUser().uid) .collection(

我正在尝试按时间戳排序查询

在我的文档中,我有一个名为“日期”的字段,其形式如下:

date = {
   nanoseconds: 963000000,
   seconds: 1594917688
}
在我的代码中,我有:

let photosArray = [];

firebase
  .getDatabase()
  .collection("photos")
  .doc(firebase.getCurrentUser().uid)
  .collection("userPhotos")
  .orderBy("date", "asc") // Sorted by date in ascending direction
  .onSnapshot((snapshot) => {
      let changes = snapshot.docChanges();
      changes.forEach((change) => {
         if (change.type === "added") {
            // Get the new photo
            const photo = change.doc.data();

            // Add the photo to the photos list
            photosArray.push(photo);
          }
       });

       // The last photo is at the top of the list
       setPhotos(photosArray);
但是当我渲染照片列表时,它们是未排序的。。。例如:第一张是2小时前拍的,第二张是1分钟前拍的,最后一张是2年前拍的

更新 这就是我在firestore中存储日期的方式

Firebase.js:

getTimestamp = () => firebase.firestore.FieldValue.serverTimestamp();
PhotoUploader.js

 await firestore
    .collection("photos")
    .doc(userId)
    .collection("userPhotos")
    .add({
        id,
        date: firebase.getTimestamp(),
     });

如果您的日期字段显示一个带有两个嵌套字段的映射,那么这实际上不是一个时间戳,并且它不会按照您期望的方式进行排序。您应该查看将日期字段添加到文档中的代码,并确保它正确使用了时间戳。或者使用一个时间戳数值,该数值将按照您期望的方式进行排序。

您的“日期”字段是否使用firebase时间戳类型?例如,如果您登录firebase控制台,当前是否将日期存储为时间戳(与数字或字符串相反)。是的,我正在使用firebase.firestore.timestamp.fromDate(新日期())我正在使用firebase.firestore.timestamp.fromDate(新日期())将日期存储为firestore中没有日期类型,而firebase.firestore.timestamp.fromDate没有日期类型(新日期())返回一个映射…shloud我使用字符串来存储日期?我也曾想过只存储秒而忽略纳秒,但它可能会工作不正确。您可以只编写一个日期对象。它将由Firestore SDK转换为Firestore时间戳。如果看不到所有相关代码,则无法看到您需要的内容我可能做错了。但是如果你看到两个字段像那样分开,你肯定做错了。我已经更新了这个问题。现在你可以看到我是如何存储日期的。问题是,在这个文档中,时间戳有我说的两个属性(秒+纳秒),但我想如果我使用orderBy(“日期”)正如我所做的那样,它将根据映射实例的某些属性进行排序,只需使用
new Date()
设置日期字段,或使用
firebase.firestore.FieldValue.serverTimestamp()
设置服务器的时间。