1

I am struggling with a uncertain issue and not able to figure out what can be the root cause.

I am calling Firebase for data using the below code:

public getAllObject(filters: Filter[]): Observable<T[]> {
  return this.afs
    .collection('somecollection')
    .snapshotChanges()
    .pipe(
      take(1),
      map((changes) => {
        return changes.map((a) => {
          console.log(a.payload.doc.data()) ==============> displays everything is alright
          const data: any = a.payload.doc.data() as T;
          const code = a.payload.doc.id;
          return { code, ...data } as T;
        });
      })
    );
}

and I am consuming the above service mentioned below:

this.service.getAllObject(this.service.getFilters()).subscribe(
  (entities) => {
    console.log(entities);==============> display's array and things are wrong.
  },
  (error) => {
    console.error(error);
  }
)

Problem description

When I call the above API, I get an array of below objects. Problem is with the stores attribute. As we move forward in the array the stores attribute contains values from pre elements. This phenomenon is HAPPENING ON CLIENT SIDE only.

I was wondering if I am using attribute name 'stores' which is any kind of reserved keyword which is causing this to happen. Or i am using rxjs in wrong way.

Current results

{ 
  code: 123,
  stores: { abc }
},
{ 
  code: 345,
  stores: { def, abc }
},
{ 
  code: 678,
  stores: { xyz, def, abc }
},

Expected results

getAllObject console.log displays the following which is correct

{ 
  code: 123,
  stores: { abc }
},
{ 
  code: 345,
  stores: { def }
},
{ 
  code: 678,
  stores: { xyz }
},

Current analysis

console.log(a.payload.doc.data());  ====> Showing correct
const data: any = a.payload.doc.data();
const code: string = a.payload.doc.id;
console.log({ code, ...data });
return { code, ...data } as T;      =====> Showing INCORRECT and adding stores from earlier element to current one.
halfer
  • 19,824
  • 17
  • 99
  • 186
Saurabh Kumar
  • 16,353
  • 49
  • 133
  • 212
  • Please edit the question to show your input data - what raw documents are you querying here? We should be able to track the inputs to the final outputs. There is nothing reserved about "stores". – Doug Stevenson May 28 '20 at 16:21
  • Does this answer your question? [Display duplicate events after update firestore data, but the data itself in firestore isn't duplicated](https://stackoverflow.com/questions/51108897/display-duplicate-events-after-update-firestore-data-but-the-data-itself-in-fir) – Józef Podlecki Jun 04 '20 at 17:14

0 Answers0