1👍
✅
To only update the user document if there are any changes, you’ll need to compare the values inside the document with the values from the form. Something like this:
//Updating User other details on submit
const db = firebase.firestore().collection("profiles");
db.doc(user.uid).get()
.then(doc => {
if (doc.exists) {
var updates = {};
if (doc.data().phonenumber != this.profile.phonenumber) updates["phonenumer"] = this.profile.phonenumber;
if (doc.data().fulladdress != this.profile.fullAddress) updates["fulladdress"] = this.profile.fullAddress;
if (doc.data().zipcode != this.profile.zipcode) updates["zipcode"] = this.profile.zipcode;
if (Object.keys(updates).length > 0) {
db.doc(user.uid).update(updates)
}
The changes I made here:
- Your code loaded all user profiles, while the above only load the profile of the current user.
- Build an
updates
object with only the values that have changed. - Only update the database if there are any changes.
Source:stackexchange.com