angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

DocumentChangeAction types are all "added" when a new document is added

Open civicBoy13 opened this issue 5 years ago • 2 comments

When i add a new document to firestore and listen for the snapshot changed, every documentchangeaction that is returned has "added" as type, as opposed to the just the newly added document.

Version info

Angular: ~11.2.0

Firebase: ^7.0 || ^8.0

AngularFire: ^6.1.5

Other (e.g. Ionic/Cordova, Node, browser, operating system): ionic: 6.13.1

How to reproduce these conditions

Some samples of my code

this is inside of my firbease service

customerCollectionsAddedListener(){ try { return this.firestore.collection("Customers").snapshotChanges(); } catch (error) { throw error; } }

async createCustomerDocument(customer: Customer): Promise { try {
this.firestore .collection('Customers') .add(JSON.parse(JSON.stringify(customer))); } catch (error) { throw error; } }


this.firebase.customerCollectionsAddedListener().subscribe(value => { value.forEach(action =>{ var payload = action.payload; console.log(action.type); console.log(payload.type) ; if(action.type === "added" && payload.doc.metadata.fromCache){ console.log(payload.doc.id); console.log(payload.doc.data()); } }); });

Debug output

Expected behavior

The expected behavior is that only the newly added document has the type of added.

Actual behavior

The actual behavior is that all the documents have a type of added.

civicBoy13 avatar Jun 13 '21 20:06 civicBoy13

Anyone have an answer ??

civicBoy13 avatar Jun 30 '21 23:06 civicBoy13

This is expected Firestore behavior rather than a bug: when you subscribe to snapshotChanges(), the initial emission delivers every existing document in the collection as an 'added' change, and only later inserts arrive individually as 'added' while edits arrive as 'modified'. So seeing all documents as 'added' on the first snapshot is correct.

I'm closing this as working as intended; if you see something different on a current @angular/fire release, please comment and we can revisit.

armando-navarro avatar Jul 23 '26 01:07 armando-navarro