Currently, I am trying to save/update data in my Firestore document. I have successfully implemented this without any issues by using an async function. However, I must admit that I am not very familiar with async functions or promises.
I have provided my code below and my main question is: Am I implementing this function correctly? Is this the proper way to update/create using an async function?
Thank you in advance for your help!
Here is the excerpt from my code:
edit_menu.ts
async onSaveClick() {
try {
this.modifyService.
updateLocationWiseMenuData(this.data.id, this.valueArray)
.then(error => {
console.log(error);
}).catch(eror => {
console.log(eror)
})
}
catch (error) {
throw error
}
}
service.ts
async updateLocationWiseMenuData(id: string, array: any[]) {
try {
if (id && array.length) {
for (const i of array) {
if (i.defaultPrice) {
await this.afs.collection(`Locations/${id}/menuList`).doc(`${i.id}`).update({
defaultPrice: i.defaultPrice
})
}
if (i.hasOwnProperty('isAvailable')) {
await this.afs.collection(`Locations/${id}/menuList`).doc(`${i.id}`).update({
isAvailable: i.isAvailable
})
}
}
}
}
catch (error) {
throw error
}
}