简体   繁体   English

如何按时间戳顺序订购快照值?

[英]How can i order a snapshot value in timestamp order?

I want to show the posts in the feed by the date it was posted, I am currently showing them by the user and that is very confusing, I would like it better if it was showing the posts by the date the post was created, I am using Firebase and Vue.js v2我想在发布日期之前显示提要中的帖子,我目前正在由用户显示它们,这非常令人困惑,我希望它能在帖子创建日期之前显示帖子,我我正在使用 Firebase 和 Vue.js v2

I know firebase stores a timestamp automatically so I am looking for a way to order the posts by that time stamp regardless of the user order, is it still posible?我知道 firebase 会自动存储一个时间戳,所以我正在寻找一种方法来根据该时间戳对帖子进行排序,而不管用户顺序如何,它仍然可行吗?

here is the code I am using to fetch the data from Firebase and the code on the component.这是我用来从 Firebase 获取数据的代码和组件上的代码。

    <template>
    <div>
           <div
           v-for="post in allPosts.slice().reverse()"
           :key="post._key">
           <v-card class=" feed-card my-3">
           <v-row no-gutters>
           <v-col cols="1">
           <v-img
           class="align-center rounded-xl "
           width="30"
           :src="post.photoURL">
           </v-img>
           </v-col>
           <v-col cols="10">
             <p class="">{{post.postText}}</p>
             <p class="blue-grey--text ">{{post.displayName}}</p>
             </v-col>
             </v-row>
       </v-card>
     </div>
   </div>
</template>
<script>
import firebase from '@/plugins/firebase'

let db = firebase.database();
//let usersRef = db.ref('users');
let postRef = db.ref('posts');

export default {
  name: 'FintechSocialFeed',
  data: () => ({
  authUser: null,
  allPosts: [] // initialise an array
}),
  methods: {
  },
  created: function() {
    data => console.log(data.user, data.credential.accessToken)
    firebase.auth().onAuthStateChanged(user => {
        if (user) {
          postRef.on('value', snapshot => {
            const val = snapshot.val()
            if (val) {
              this.allPosts = Object.values(val).flatMap(posts =>
              Object.entries(posts).map(([ _key, post ]) => ({ _key, ...post})))
            }
            console.log(snapshot.val())
          });
        }

     })
   }
}
</script>

here is a little image of how it looks, first is Abraham's posts then the jake's so I want it to be by date not by user order.这是它的外观的小图片,首先是 Abraham 的帖子,然后是 jake 的帖子,所以我希望它按日期显示,而不是按用户顺序显示。

在此处输入图像描述

Firestore can only order/filter data on values that it has indexes for. Firestore 只能根据它具有索引的值来排序/过滤数据。 While it may also store additional metadata for its own purposes, this values are typically not indexed (the document ID being the one exception to that), and thus can't be used in queries.虽然它也可能出于自己的目的存储额外的元数据,但这些值通常没有索引(文档 ID 是一个例外),因此不能在查询中使用。

Thus there is no way to order Firestore results based on the internal timestamp.因此无法根据内部时间戳对 Firestore 结果进行排序。 If you want to be able to order documents on a timestamp, you'll have to store that timestamp as a field in the document, and then pass that field name to orderBy .如果您希望能够根据时间戳对文档进行排序,则必须将该时间戳存储为文档中的一个字段,然后将该字段名称传递给orderBy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM