Vuefire: References in arrays and non-existent are lost when updating any field in a document

Created on 2 Aug 2019  路  12Comments  路  Source: vuejs/vuefire

Thanks for making Vuefire, it's great!

I have a bound object that has an array of references as one of the fields:
image

For existing references in the array everything works well, however thing break when I add a new entry into that array with

firestore.collection("logs").doc(this.log.id)
    .update({
        photos: 
            firebase.firestore.FieldValue.arrayUnion(doc.ref)
    });
});

I end up with the right data inside firestore, but in Vue, the new entry in the array is loaded as a string instead of an Object:
image

If I re-bind the object, everything is fine.

bug firestore

Most helpful comment

I found the problem and a solution. I need to refactor some of the code and write unit tests before releasing a new version. I will probably be able to in a week or so 馃檪

All 12 comments

I tested a bit more and it turns out to happen no matter what field is updated. It happens to references of documents that do not exist or references inside objects and arrays

I have the same issue with vuexfire. When i add a new reference to array of references (for example with Functions), only the last added reference will be an object.

After Init:
image

After new reference added:
image

I have the exact same problem to deal with.
Does anybody have a workaround or the correct way to manipulate the data?

Any official word on this? I am encountering the same issue and am interested in a fix or the proper way to manipulate the data without calling .update on the document directly.

@posva I am also encountering this issue. Any news on this?

We were actually able to solve this issue by rebinding to the collection after calling update.

db.collection('users').doc(this.user.username).update({ viewedCompanyProfileIds: currentViewedCompanyProfileIds }) .then(() => { this.bindUserDoc() })

and

bindUserDoc: firestoreAction( ({bindFirestoreRef, state, commit}) => { return bindFirestoreRef('userDoc', db.collection('users').doc(state.user.username)).then( () => { commit('setViewedCompanyProfileIds', state.userDoc.viewedCompanyProfileIds || []) }) } )

This may be overly tedious if you're using this pattern a lot, and it may have problems that I'm not aware of, but it did resolve our issue without having to make changes to the library.

We were actually able to solve this issue by rebinding to the collection after calling update.

db.collection('users').doc(this.user.username).update({ viewedCompanyProfileIds: currentViewedCompanyProfileIds }) .then(() => { this.bindUserDoc() })

and

bindUserDoc: firestoreAction( ({bindFirestoreRef, state, commit}) => { return bindFirestoreRef('userDoc', db.collection('users').doc(state.user.username)).then( () => { commit('setViewedCompanyProfileIds', state.userDoc.viewedCompanyProfileIds || []) }) } )

This may be overly tedious if you're using this pattern a lot, and it may have problems that I'm not aware of, but it did resolve our issue without having to make changes to the library.

In my case, the binding is coming from a higher component. Not connected with the one that do the changes. To implement your solution I would need to use Vuex Store or a chain of events.

The solution from the library would be great! Since it`s already incredible the quality of the work done.

After some analysis, I'm not so "into" your code yet, but I did some tests and found some possible reasons.

The first bind is done, all the refs are resolved and bind.
When any change happens at any document field that has a DocRef inside an array it is unsubscribing all the old refs, since they are not in the missing list. Then, only the new refs if included are being subscribed. This included ones get the info resolved from the ref and the old ones not.

I'm studying the code to understand exactly which part these changes are being unsubscribed. But if you @posva have any clue by the mind of where it can be, please tell me that I will try to work on it.

@marco-quintella Unfortunately I haven't been able to take a look so I don't have a clue beyond of where the problem is beyond the ref diffing you mentioned. So I think you are on a good track!

@posva & @marco-quintella I am also encountering this issue. Any news on this?

I found the problem and a solution. I need to refactor some of the code and write unit tests before releasing a new version. I will probably be able to in a week or so 馃檪

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joelxr picture joelxr  路  3Comments

dev-kr2020 picture dev-kr2020  路  4Comments

SonarBeserk picture SonarBeserk  路  4Comments

calirails picture calirails  路  5Comments

franciscolourenco picture franciscolourenco  路  7Comments