【问题标题】:Javascript .sort by time not working on array?Javascript .sort 按时间排序不适用于数组?
【发布时间】:2021-04-16 23:19:20
【问题描述】:

我有一个平面列表,其中显示了当前用户关注的所有用户帖子。这在 instagram、twitter、所有社交网络等中都很正常。

我想按时间顺序显示它们,但它不起作用。

代码是这样工作的:

  1. 我在 Firestore 中查询当前用户的帖子
  2. 我有一个当前用户关注的用户的 UID 列表
  3. 我在 Firestore 中查询当前用户关注的任何人的帖子
  4. 这会返回我想要的所有帖子
  5. 帖子以块为单位。例如,当前用户的帖子被添加到数组中。然后 user1 当前用户正在关注的帖子被添加。然后 user2 的帖子被添加。等等。
  6. 我尝试运行 Javascript 提供的 .sort 函数来按时间顺序排列帖子

这是它的代码(删除了文档字段,因为它们不重要,除了 date_created):

getCollection = async (querySnapshot) => {
    const followingPosts = [];

    await Firebase.firestore() <----------------------- Get current users posts
    .collection('globalPosts')
    .where("uid", "==", Firebase.auth().currentUser.uid)
    .onSnapshot(function(query) {
        query.forEach((doc) =>  {
            const { 
                ....other fields

                date_created

                ....other fields
                } = doc.data();
    
                followingPosts.push({
                    ....other fields

                    date_created

                    ....other fields
                });
        })
    
    });

    querySnapshot.forEach(async (res) => {
        
        await Firebase.firestore() <-------------- get following users posts, uid after uid
        .collection('globalPosts')
        .where("uid", "==", res.data().uid)
        .onSnapshot(function(query) {
            query.forEach((doc) =>  {
                const { 
                    ....
                    date_created
                    ....
                    } = doc.data();
        
                    followingPosts.push({
                        ....other fields

                        date_created

                        ....other fields
                    });
            })

            
        
        });

         
    });
      
    
    followingPosts.sort(function(a,b){ <-------- How I try to sort the posts by date created

            return a.date_created.toDate() - b.date_created.toDate()

    })

    this.setState({
        followingPosts,
        isLoading: false,   
    });

}

几点说明:

  1. 帖子获取正确(仅显示当前用户关注的帖子的人)

  2. 我之所以这样做 date_created.toDate() 是因为firestore时间戳对象以纳秒和毫秒为单位。无论我有 date_created.toDate() 还是只有 date_created,它都不起作用。

  3. 我知道我可以按 date_created 查询 firestore 和 order,在查询中降序排列。但是由于帖子是按顺序查询的,所以这只对单个帖子块进行排序,而不是对整个数组进行排序

  4. 我尝试将 followerPosts.sort 函数放在查询快照中,在 for each 之后。也不工作:

        querySnapshot.forEach(async (res) => {
    
        await Firebase.firestore() 
        .collection('globalPosts')
        .where("uid", "==", res.data().uid)
        .onSnapshot(function(query) {
            query.forEach((doc) =>  {
                const { 
                    ....other fields
    
                    date_created
    
                    ....other fields
    
                    } = doc.data();
    
                    followingPosts.push({
    
                        ....other fields
    
                        date_created
    
                        ....other fields
                    });
            })
    
    
    
        });
    
     followingPosts.sort(function(a,b){ 
    
            return a.date_created.toDate() - b.date_created.toDate()
    
    })
    
    });
    

编辑:关于 date_created 的更多信息:

  1. 创建后(向 firestore 添加新帖子),date_created 的初始化如下:

        date_created: new Date()
    
  2. 在firestore中,上述初始化创建日期的方法如下:

  1. 当我在控制台记录 date_created 时,会返回一个 firestore 时间戳对象:

    t { “纳秒”:14000000, “秒”:1610413574, }

  2. 这对我的目的来说是不可用的,所以当我将数据传递给平面列表时,我使用 .toDate() 转换了这个时间戳对象:

        <FeedCellClass 
            ... other fields
            date_created={item.date_created.toDate()}
        />
    
  3. .toDate() 将其转换为这个,我可以将其用于我的目的:

    2021-01-12T01:06:14.014Z

让我知道如何解决这个问题。

【问题讨论】:

  • followingPosts 在您对其进行排序之前的输出是什么?我只是在检查以确保其中确实有元素。我不确定你得到的错误是什么,是空的还是你得到的数据不按顺序。
  • @MichaelBauer Ahh,我认为您在这里有所作为。我在调用firestore之后添加了console.log(followingPosts),在我设置状态之前,它输出了Array []。我只能想到一件事:对 firestore 的查询是异步的,因此它不会对数组进行排序,因为它是空白的。但这并不能解释为什么当我在查询快照中添加排序时它不起作用
  • @MichaelBauer 解决了问题,添加了答案。你的 cmets 有帮助!!谢谢

标签: javascript react-native google-cloud-firestore


【解决方案1】:

我解决了我的问题 - 我在错误的地方排序。这是解决方案:

    querySnapshot.forEach(async (res) => {
        
        await Firebase.firestore()
        .collection('globalPosts')
        .where("uid", "==", res.data().uid)
        .onSnapshot(function(query) {
            query.forEach((doc) =>  {
                const { 
                    ...
                    date_created
                    } = doc.data();

        
                    followingPosts.push({
                        ...
                        date_created
                    });
                
            }) <----------- I put it in the on snapshot, instead of after. 

            followingPosts.sort(function(a,b){ 

                return b.date_created.toDate() - a.date_created.toDate()
        
            })
        
        });

    });

谢谢大家

【讨论】:

  • 另外,执行 a.date_created - b.date_created 会按升序对其进行排序。 d.date_created - a.date_created 按降序对其进行排序。
猜你喜欢
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多