Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

344
Views
Vue Firestore query. How to avoid duplication when Firestore updates data?

In an ionic vue app, I want to display some data from a Firebase Firestore in.

In the Firestore, I have a collection households with a subcollection entries. A user can be a member of a household and I want to display all entries of all households she is a member of.

I have an array entries in my data:

data() {
  return {
    entries: [],
  };
},

I fill this array by making three nested queries against firebase:

  1. Get current user
  2. Get all households associated with current user
  3. Get all entries from all households for current user
beforeCreate: function () {
  auth.onAuthStateChanged((user) => {
    if (user) {
      // Get current uid
      let uid = user.uid;

      // Loop trough all households of user
      let userHouseholdIds = [];
      db.collection("households")
        .where("members", "array-contains", uid)
        .onSnapshot((snapshotChange) => {
          snapshotChange.forEach((doc) => {
            userHouseholdIds.push(doc.id);
          });

          userHouseholdIds.forEach((householdId) => {
            // Get household name
            db.collection("households")
              .doc(householdId)
              .get()
              .then((value) => {
                let householdId = value.id;
                let householdName = value.data().name;

                // Get household entries
                db.collection("households")
                  .doc(householdId)
                  .collection("entries")
                  .onSnapshot((snapshotChange) => {
                    snapshotChange.forEach((doc) => {
                      let newDoc = doc.data();
                      newDoc["id"] = doc.id;
                      newDoc["householdName"] = householdName;
                      newDoc["householdId"] = householdId;
                      this.entries.push(newDoc);
                    });
                  });
              });
          });
        });
    }
  });

Although I'm not sure if this is the way to go, this works as expected (so far).

However, when I make a change in an entry e1 in a household h1 directly in the Firestore console, every entry from h1 gets duplicated in the frontend.

I guess, I need a this.entries = [] somewhere before an update from Firestore comes in, but I cannot figure out where.

Am I on the right track? What would I need to modify to avoid the duplication? Thanks a lot!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

if you have to use 'onSnapshot' check change.type

db
.collection(...)
.where(...)
.onSnapshot((snapshotChange) => {
    snapshotChange.docChanges().forEach(change => {
        if (change.type === 'added') {
          this.entries.push()
        } else if (change.type === 'modified') {
          this.entries.splice()
        } else if (change.type === 'removed') {
          this.entries.splice()
        }
      })
})

If not, try using get()

db
.collection(...)
.where(...)
.get()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!