当值更改Firebase Vue时检索孩子

时间:2019-03-05 09:31:46

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

我正在使用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')
        },

状态为真后,帖子必须在墙上页面中可见。

如何在墙页面中实时获取状态为== 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”,但是我需要一直刷新页面以查看新帖子。

这是我的数据库

enter image description here

1 个答案:

答案 0 :(得分:0)

您应该使用以下方法来过滤数据,而不是使用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);
            })