【发布时间】:2019-03-03 14:45:54
【问题描述】:
我正在尝试将一些数据保存/更新到我的 firestore 文档中。我已成功实现它,没有任何问题。要保存数据,我正在使用异步函数。但我对异步函数或承诺不太熟悉。 我在下面发布了我的代码,我的问题是,我是否正确实现了该功能?这是使用异步函数实现更新/创建的正确方法吗?
提前致谢
这是我的代码;
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
}
}
【问题讨论】:
标签: typescript google-cloud-firestore angular6