1

I am updating the information in my database and I want that data to update the Cards I am using to display the data RealTime. This works fine but when I make a change to the database the cards I am using to display the info just duplicate on every change made.

I Want it to replace/update the Cards I am displaying But it makes a new card and adds it to the screen along with all old cards on every change.

Pretty new to Vue and Firebase cant seem to get it to work.

<template>
<v-flex xs12 sm6 md4 lg3 xlg3 v-for="user in users" :key="user.id">  
          <v-card raised :class="`${user.Status} text-xs-center ma-3`" >            
            <v-responsive align="middle" class="pt-4">
              <v-icon :class=" `${user.Status} ma-n1 mb-n1`" size="55px" >account_box</v-icon>
            </v-responsive>
            <v-card-text>
              <div align="middle" class="subheading White">{{user.Name}}</div>
              <div align="middle" class="grey--text ma-1">
                <v-chip color="" small ripple  :class="`${user.Status} caption my-3`"><div class="White mr-2">Status: </div><div :class="`${user.Status}`"> {{user.Status}}</div> </v-chip>
              </div>
              <v-divider></v-divider>
              <div align="middle" class="White ma-1">Unit:  {{user.GroupHex}} - {{user.UserIDInt}} </div>          
              <div align="middle" class="grey--text mt-3">Last Message:<div class="Green">{{user.TimeStamp}}</div></div>       
            </v-card-text>
            <v-card-actions raised align="center">
              <v-spacer></v-spacer>
              <router-link class="noLink" :to="{ path: '/Logs'}" append>
             <v-btn color=" " small align="middle" flat class="" >
                <v-icon  color="grey" class="mr-2">assessment</v-icon>
                <span>Log Data</span>
               </v-btn>
               </router-link>
              <v-spacer></v-spacer>
            </v-card-actions>
          </v-card>
        </v-flex>

</v-layout>
<NewEmployee />
</v-container>
  </v-app>

</template>

<script>

import firestore from "firestore";
// eslint-disable-next-line
import EmployeePop from "@/components/updatePopups/EmployeePop";
import NewEmployee from "@/components/updatePopups/NewEmployee";
import db from "@/components/fbInit";
import firebase from "firebase";

export default {
// eslint-disable-next-line
  components: {EmployeePop, NewEmployee},
  data() {
    return {
      users: [],
      links: [
      { route: "/logs" },
      ]
    };
  },

  created() {

    var user = firebase.auth().currentUser;
    var employeeRef = db.collection('userProfiles').doc(user.uid).collection('employees')

   employeeRef.onSnapshot(snap => {
      snap.forEach(doc => {
        const data = {

         id: doc.id,
            Name: doc.data().Name,
            GroupHex: doc.data().GroupHex,
            UserIDInt: doc.data().UserIDInt,
            SuperID: doc.data().SuperID,
            misc: doc.data().misc,
            Status: doc.data().Status,
            TimeStamp: doc.data().TimeStamp,
            Original: doc.data().Original


          }
  this.users.push(data);
       });
    })      
  },
</script>

Duplicates v-cards instead of replacing with new data.

Yewla
  • 325
  • 3
  • 21

1 Answers1

7

This is because each time a document is added to the collection the listener receives the entire set of documents of the collection and you push those documents to the users array without re-initializing the array.

You can verify that by adding a console.log() as follows, which will log the snapshot's size:

   employeeRef.onSnapshot(snap => {
      console.log(snap.size);  <-- Add here
      snap.forEach(doc => {...})
      //...
   });

You could re-initialize the users array each time the listener is triggered, as follows:

   employeeRef.onSnapshot(snap => {
      this.users = [];  <-- re-initialize the array
      snap.forEach(doc => {...})
      //.....
   });

But there is a better solution, as explained in the doc: to analyse "the actual changes to query results between query snapshots".

For example, for catching only the documents that are added to the collection, you would do as follows:

  created() {
    const user = firebase.auth().currentUser;
    const employeeRef = db.collection('userProfiles').doc(user.uid).collection('employees')


    employeeRef.onSnapshot(snap => {
      snap.docChanges().forEach(change => {
        if (change.type === "added") {
          const data = {
            id: change.doc.id,
            Name: change.doc.data().Name,
            ....
          };
          this.users.push(data);
        }
      });
    });
  }
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121