Two appearances of the record indicate that it has been updated

Whenever I update a record, it ends up appearing twice on the screen.

This is how I am loading it:

   ngOnInit() {
    this.loadItems();
    }
    
      loadItems(){
        this.first_time_enter = true;
        console.log(this.first_time_enter);
        this.tableData = [];   
      let query3 =  firebase.firestore().collection('complaints').where('complaint_status', '==', "Open")
       query3
       .orderBy("complaint_date","desc").limit(2)
       .onSnapshot(querySnapshot=> {  
         querySnapshot.forEach(doc=> {
                      var data = Object.assign(doc.data(), {docid : doc.id}, {duration: duration})
             this.tableData.push(data);
             console.log(this.tableData);
         });
}

The content from this.tableData array is displayed on the html page.

Upon page load, I can see 2 entries and if there is an update made through the Firebase console, they increase to 4. How can I handle this situation? I suspect that this.tableData.push(data); might be causing this issue, even though I clear the array using this.tableData = [] before the query, which doesn't seem to resolve the problem.

Please refer to the images below for better understanding:

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

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

Answer №1

Whenever there are updates to the complaints, your callback function is triggered with a querySnapshot that contains all complaints, even those that have not been modified.

You can choose to only handle the documents that have changed, or you can clear out your tableData. The latter is straightforward:

  let query3 =  firebase.firestore().collection('complaints').where('complaint_status', '==', "Open")
   query3
   .orderBy("complaint_date","desc").limit(2)
   .onSnapshot(querySnapshot=> {  
     this.tableData = []; // Clear previous data
     querySnapshot.forEach(doc=> {
                  var data = Object.assign(doc.data(), {docid : doc.id}, {duration: duration})
         this.tableData.push(data);
         console.log(this.tableData);
     });

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

A guide on transferring files to a PHP server using HttpClient and FormData within an Ionic framework, along with instructions on receiving files in PHP

I have an input file in mypage.html on Ionic and I want to send this file to PHP for uploading it onto the server. In mypage.page.html, I have created an input field for the file name and another input field for the file to be uploaded. <ion-header> ...

Using Angular 2 for form validation

Hey there, I'm currently using Angular 2 and I need to set the password field requirements to include at least 1 uppercase letter, 1 lowercase letter, 1 number, 1 special character with a minimum of 8 characters and maximum of 16 characters. So far, ...

Participating in consolidated data

I need to merge data from my trainings-table with my users-table. The structure of the data is as follows: - users - key1 - name - trainingIds - t_key1 - t_key3 - trainings - t_key1 - name - description - t_k ...

The parameter cannot be assigned the readonly type 'X' in this context

I encountered an issue with a third-party library where the class structure changed. Initially, it was defined as: export class Foo { field: X[]; …. } In my code, I was working with this type: print(foo.field) After updating to a new version, the c ...

What steps do I need to take in order to ensure that each webpage displays unique thumbnails?

As a newcomer to website development, I recently looked into implementing open graph on my site. However, I ran into an issue where I could only set one thumbnail for the entire website. This posed a problem as I wanted each navigation menu tab (Home, Abou ...

Ways to modify the input field value in my form based on the current page context

I am currently developing a website for a sports event organization company using Angular, HTML/CSS. The focus of this website is on the triathlon sport and it consists of several stages. On the home page, there are five image tags representing each stage ...

Adjusting canvas height in Storybook - Component does not fit properly due to low canvas height

I had a component that I needed to add to Storybook. It was working fine, but the styling was slightly off. I managed to resolve this by adding inline styling with position: absolute. Here is how it looks now: const Template: any = (args: any): any => ( ...

The function signature '(_event: React.SyntheticEvent, value: number) => void' cannot be assigned to the type 'FormEventHandler<HTMLUListElement>'

I am facing an issue with my component "PageFooter" being duplicated in three other components. I am trying to refactor it as a UI component. " I am getting the error message: 'Type '(_event: React.SyntheticEvent, value: number) = ...

Testing a feature in Angular that modifies a variable

Trying to test a function that updates a Boolean variable has been causing some confusion for me. The strange thing is, even though the function seems to be called successfully when using the toHaveBeenCalled method, the variable itself never actually gets ...

Angular class requires multiple class members and validators for MatSelection List to be bound with Formbuilder

Could you please guide me on how to connect the Google Angular Materials mat-selection-list with the FormBuilder? We have the following class and are attempting to utilize Reactive Form Builder for this purpose. While we are aware of how to link data class ...

The validation process in reactive forms is experiencing some issues with efficiency

Trying to debug an issue with my reactive forms - the repeatPassword field doesn't update as expected. When entering information in the "password" field, then the "repeatPassword" field, and back to "password", the second entry is not flagged as inval ...

"Dealing with conflicts between RMQ and TypeORM in a NestJS

Every time I try to use TypeOrm, RMQ crashes. I can't figure out why. Utilizing the library golevelup/nestjs-rabbitmq has been a struggle for me. I've spent 7 hours trying to resolve this issue. @Module({ imports: [ ConfigModule.f ...

Ways to set a default value for Subject RxJS observable

Currently working on developing an Angular 2 application where I need to enable the reordering of columns in a List by clicking on the title, similar to how data-tables work. I came across some examples in the Angularfire 2 documentation which gave me an i ...

Creating a large and spacious modal in Angular using ngx-bootstrap

I need help with resizing the ngx-modal to cover a large area of the screen. Currently, I am trying to make it so that when something is clicked, the modal should overlay an 80% width grid on a full desktop screen. Despite my attempts at using .modal-xl an ...

Utilizing React Typescript for Passing Props and Implementing them in Child Components

I'm currently working with React and TypeScript and attempting to pass data as props to a child component for use. However, I've encountered an error that I can't quite understand why it's happening or how to resolve it. Additionally, I ...

What could be causing TypeScript to throw errors when attempting to utilize refs in React?

Currently, I am utilizing the ref to implement animations on scroll. const foo = () => { if (!ref.current) return; const rect = ref.current.getBoundingClientRect(); setAnimClass( rect.top >= 0 && rect.bottom <= window.i ...

Guide to retrieving static information for forms with the use of reactive forms

I am encountering an issue with my reactive form. When I click the new button, a duplicate section appears but the options in the select field are not visible. Additionally, I receive errors as soon as the page loads: ERROR Error: Cannot find control with ...

Implementing shared element route transitions using framer-motion and NextJS (written in typescript)

I'm having trouble implementing animated routing using the <AnimateSharedLayout /> component from framer-motion. What I'm trying to achieve in the code below is to show a list of images and, upon clicking on them, navigate to /images/[image ...

CSS code for a fixed fullscreen menu with scrolling

I have implemented a full-screen menu that covers the entire screen, excluding the header and menu tab bar. You can view it here: https://i.stack.imgur.com/h5cQa.png On smaller screens, the entire menu cannot be displayed at once, as shown here: https://i. ...

Guide to summing the values in an input box with TypeScript

https://i.stack.imgur.com/ezzVQ.png I am trying to calculate the total value of apple, orange, and mango and display it. Below is the code I have attempted: <div class="row col-12 " ngModelGroup="cntMap"> <div class="form-group col-6"> ...