【问题标题】:Retrieve child just when value change firebase vue仅在值更改时检索孩子 firebase vue
【发布时间】:2019-03-05 09:31:45
【问题描述】:

我正在使用 Vue 和 Firebase 开发我的第一个应用程序。

在一个页面中,您可以创建一个帖子并将其发送到实时数据库名称电子邮件图片和状态。

在另一个页面(管理员页面)中,您决定是否发布该帖子或不将状态更改为 true 或 false。 所以:

 reading() {
            this.postRef.on('child_added', snapshot => {
                this.posts.push(snapshot.val());
                this.key.push(snapshot.key);
            }), 
changeStatus(index) {
            let key = this.key[index]
            this.posts[index].status = true;
            firebase.database().ref('posts/'+ key +'/status').set('true')
        },

一旦状态为真,帖子必须在墙页中可见。

如何在墙页中实时检索status == true的孩子? 现在我正在使用这种方法:

 reading() {
            this.postRef.on('child_added', snapshot => {
                this.posts.push(snapshot.val());
                this.key.push(snapshot.key);
            }) 

在模板中:

 <v-layout row wrap justify-center v-for="(item, index) in posts">

我已尝试使用 v-if="item.status == true" 但我需要一直刷新页面才能看到新帖子。

这是我的数据库

【问题讨论】:

    标签: javascript firebase vue.js firebase-realtime-database


    【解决方案1】:

    您应该过滤数据,而不是使用on() 方法来观察整个 postRef 节点中的事件,如下所述:https://firebase.google.com/docs/database/web/lists-of-data#filtering_data

    因此,您可以执行以下操作,以过滤数据并仅侦听与从 status == falsestatus == true 的更改相对应的事件:

    reading() {
                this.postRef.orderByChild('status').equalTo(true).on('child_added', snapshot => {
                    this.posts.push(snapshot.val());
                    this.key.push(snapshot.key);
                })
    

    【讨论】:

      猜你喜欢
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 2021-03-11
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多