Find the child only when the value changes in Firebase using Vue

I'm currently developing my first application using Vue and Firebase.

On one page, users can create a post and send it to the real-time database with name, email, picture, and status.

In another page (the admin page), you have the option to publish or not publish a post by changing its status to true or false. So:

 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')
        },

Once the status is set to true, posts should be visible on the wall page.

How can I retrieve only the children with status == true in real-time on the wall page? Currently, I am using this method:

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

And in the template:

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

I have tried using v-if="item.status == true" but I need to refresh the page constantly to see new posts.

Here is a screenshot of my database:

https://i.sstatic.net/jfUcQ.png

Answer №1

To optimize event observation in the complete postRef node, it is recommended to filter the data according to this guide: https://firebase.google.com/docs/database/web/lists-of-data#filtering_data

A more efficient approach would be to specifically monitor changes from status == false to status == true, such as:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to connect List to dropdown in angular

Having trouble connecting dropdown with a List in AngularJS: <select> <option ng-repeat="x in list">{{x}}</option> </select> app.controller('myCtrl', function($scope) { $Scope.list=[{id:1, name='name 1' ...

The values in each row of the table are dynamically updated according to the option selected in the dropdown

I am working on a project to create a dynamic page with a form that includes Total and Converted Total Value fields. The total value is generated through a PHP do-while loop. The page also features a currency dropdown at the top. When a currency is selecte ...

How can you store JavaScript objects to files, including their methods, in a Node.js environment?

In reading about saving objects to files in Node.js here on Stack Overflow, I understand the process. However, I am curious if there is a method that allows for writing an object in a manner that would enable me to reload the object into memory along wit ...

Using Vue to Send Component Data to the User Interface

I am exploring Vue and trying to create a website with a Facebook login feature. I have a Facebook login component that is functioning properly, but I am struggling to make the acquired fbid and fbname available outside of the component in my Vues. I under ...

The Vue component table is not showing any retrieved data

Seeking help as a newcomer to the world of Laravel and Vue.js. Trying to populate data on a Vue component, but facing an issue with the tableData variable in the axios.get response. It is not rendering the array data onto the table as expected. Could there ...

An error occurred due to a state update being attempted on an unmounted component. The solution is to properly cancel all subscriptions and asynchronous tasks in a

Currently, I am attempting to utilize ListEmptyComponent within a FlatList. If there is no data present, I intend to display ListEmptyComponent={} However, in the Loadingsecond component, I am using useEffect to render when loading is true; if there is s ...

Jstree: Whenever I refresh my tree, the checkboxes do not reset to default and remain unchecked

After attempting to implement the following code: $("#jstree_demo_div").jstree("destroy").empty(); Although it successfully removes the checked nodes and reloads the tree, it fails to apply the new changes. Any advice on how to resolve this issue would b ...

What is the best way to retrieve a value from an asynchronous function in Node.js?

var fs = require('fs'); var ytdl = require('ytdl-core'); var favicon = require('serve-favicon'); var express = require('express'); var app = express(); app.use(favicon(__dirname + '/public/favicon.png')); ...

Exploring methods to deactivate specific dates within the angular material datepicker

Is it possible to prevent specific dates from being selected based on the current date? For example, if today is 8/1/16, I would like to disable the next 4 days (any number of days could be chosen), excluding weekends. So, if today is 8/1/16, I would want ...

Can someone guide me on implementing Node.js clusters in my basic Express application?

— I have successfully developed a basic application that retrieves data (50 items) from a Redis DB and displays it on localhost. After running an ApacheBench test with parameters c = 100, n = 50000, I am achieving around 150 requests/sec on my aging dual ...

The Angular $http.post() successfully communicates with the server, however, it does not trigger any alerts within

Currently facing an issue with my angular code. I am using $http.post to send data to a php file that then saves the information into a database. Below is my controller: app.controller('send_data', function($scope, $http) { $http.defaults.he ...

Why does it appear that Angular is undefined in this straightforward Angular demonstration?

I'm completely new to AngularJS and I've encountered an issue. Yesterday, I ran a very simple AngularJS application that I downloaded from a tutorial and it worked perfectly. The application consists of 2 files: 1) index.htm: <!DOCTYPE htm ...

Enhancing Material UI KeyboardDatePicker with personalized CSS design

Material UI KeyboardDatePicker is the component I'm currently using. https://i.sstatic.net/It50L.png In order to remove the black line visible in the datepicker (as shown in the screenshot), what steps should I take? Displayed below is the code ...

Guide on deactivating the div in angular using ngClass based on a boolean value

displayData = [ { status: 'CLOSED', ack: false }, { status: 'ESCALATED', ack: false }, { status: 'ACK', ack: false }, { status: 'ACK', ack: true }, { status: 'NEW', ack ...

The text box's word limiter is malfunctioning when writing code on a child page of the master

Encountering an unexpected problem that I never thought would happen. I wrote a word limiter code for a text box on a single page and it was working perfectly fine. The word limiter worked and the remaining words were displayed by a label named "remaining6 ...

modify the structure of an object according to specific conditions

So, I have this object that looks like the following. I'm currently working on restructuring the object based on the parent and child relationship. var b = []; for(var i=0;i<a.length;i++){ if(a[i].parent == null){ const children = a.filter(x => ...

Is it possible for React and React Router to render the same element twice with different props, creating two elements with identical props?

Currently, I am exploring how to leverage React in conjunction with React Router v4 to display multiple elements. The concept revolves around a component that showcases a list of articles sourced from various websites. Each route corresponds to a distinct ...

Optimizing image centering in Next JS as screen size expands

I've been struggling to work with NextJS's Image component. My goal is to set up a banner image that only covers a specific amount of space on the screen, regardless of screen size. I have managed to achieve this by using max-height: 30vh and ov ...

How to smoothly scroll to a specific div on click and hide the top div using Jquery

Snippet of Code: HTML Markup: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <title>Title</title> </head> <body> <div id="intro"> <a href="#" id="showfullsite"></ ...

Passing values between a JavaScript function and a PHP script - Here's how!

I am looking to display the output of a PHP script in a div tag without having to refresh the page. I have a collection of .c files that users can compile on the server, and I want to list them in a dropdown like this: <option value="" selected="select ...