简体   繁体   English

我如何按包含 vue.js 的时间戳索引的快照的子数据进行排序

[英]how can i order by snapshot's child data containing a timestamp index with vue js

I saved the timestamp as a data field inside firebase DB, this way I can now retrieve it like {{ post.timestamp }} to display it, how would I go from where I am at to order the posts by timestamp order regardless of the user object order, for example, what I get in the UI is the posts ordered by the user and not by time.我将时间戳保存为 firebase 数据库中的数据字段,这样我现在可以像 {{ post.timestamp }} 一样检索它来显示它,我将如何从我所在的位置 go 按时间戳顺序排序帖子,而不管user object order 例如,我在UI中得到的是用户排序的帖子,而不是时间。

data on firebase looks like this: firebase 上的数据如下所示:

在此处输入图像描述

Code looks like this:代码如下所示:

    <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>
             <p class="mb-n1 mt-n5 d-flex flex-row-reverse"> {{post.date}} {{post.time}}</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 the UI showing the latest post at the bottom because it is sorting by the user and not date:这是在底部显示最新帖子的用户界面,因为它是按用户而不是日期排序的:

在此处输入图像描述

I don't use firebase, but it looks like db reference provides orderByKey , so...我不使用 firebase,但它看起来像数据库参考提供orderByKey ,所以......

let postRef = db.ref('posts').orderByKey('timestamp');

An alternative would be sorting yourself, after retrieval...另一种方法是在检索后对自己进行分类......

this.allPosts = Object.values(val).flatMap(posts =>
    Object.entries(posts).map(([ _key, post ]) => ({ _key, ...post}))
).sort((a, b) => a.timestamp.toMillis() - b.timestamp.toMillis()); 

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

相关问题 如何按时间戳顺序订购快照值? - How can i order a snapshot value in timestamp order? 如何在 HTML 中显示快照数据? - How can I display the snapshot data in HTML? 我如何更改 firebase 上快照中的数据 - how can i change the data inside the snapshot on firebase 我可以使用 GCP 快照进行数据或数据库备份吗? 我可以信任快照来恢复我的数据库数据吗? - Can I use GCP Snapshot for data or database backup? can I trust the snapshot to restore my DB data? Firebase 如何在 js 上按孩子排序 - Firebase how to Order by child on js 如何下载 AWS Postgres 数据库的 S3 快照的本地副本? - How can i download a local copy of an S3 snapshot of an AWS Postgres DB? 如何在 onSnapshot() 中使用 orderBy() 以便我可以根据时间戳对文档进行排序? - how to use orderBy() in onSnapshot() so I can order the documents on the basis of the timestamp? 我可以从 S3 中的本地快照启动 MariaDB RDS 实例吗? - Can I launch a MariaDB RDS instance from a local snapshot in S3? 如何在我的 RecyclerView 中显示来自 firebase 的子数据? - How do I display child of child's data from firebase in my RecyclerView? 如何从 firebase 数据快照中获取特定的 snapkey - How do I get a specific snapkey from firebase data snapshot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM