【发布时间】:2018-09-25 19:05:10
【问题描述】:
我正面临一个非常奇怪的情况。第 2/3 行代码在第 1 行代码之前执行,我认为代码不是异步的。
这是我的构造函数的样子:
this.menu = this.navParams.get('menu')
console.log(this.menu) //code line 31
为了确保页面加载后菜单值仍然相同,我在ionViewDidLoad() 的console.log - 第38 行,结果是相同的。您可以看到此时没有“approvedBy”。
所以现在我执行一个函数,就是approveMenu()
approveMenu() {
console.log(this.menu) //line 52
let newMenu = this.menu //line 54
console.log(newMenu) //line 55
newMenu['approvedBy'] = this.currentUser.uid //line 56
console.log(newMenu) //line 57
}
但是,在我执行该函数后,"approvedBy" 将在我放入菜单之前添加到菜单中。
大家可以参考下面的截图,console.log的顺序是对的,但是第52行和第54行console.log应该没有"approvedBy"吧?它应该只出现在第 57 行,因为 "approvedBy" 在第 56 行添加到 menu。
但显然它并没有按预期工作,请赐教。
已编辑
感谢 PierreDuc 解决了我的问题。
之后,使用相同的功能,它调用我的提供程序来更新我的数据库。这里是approveMenu()的扩展
approveMenu() {
console.log(JSON.parse(JSON.stringify(this.menu)))
let newMenu = this.menu
console.log(JSON.parse(JSON.stringify(newMenu)))
newMenu['approvedBy'] = this.currentUser.uid
console.log(Object.assign({}, newMenu))
console.log(Object.assign({}, this.menu))
this.restaurantProvider.approvePendingMenu(this.menu).then(()=>{
this.alertProvider.successful('Successful','The menu has been added to your restaurant and it can be view by public now','Noted')
this.navCtrl.pop()
}).catch(err=>{
this.alertProvider.successful('Something Went Wrong',err,'Noted')
})
}
当我调用restaurantProvider 的approvePendingMenu(this.menu) 函数时,有些代码没有按照我的预期处理。我先给你看approvePendingMenu(this.menu):
approvePendingMenu(menu){
console.log(Object.assign({}, menu))
return this.pendingSuggestiontRef.child(menu.suggestId).remove().then(()=>{
console.log(Object.assign({}, menu)) //line 63
delete menu.suggestId
console.log(Object.assign({}, menu)) //line 65
const newMenu = menu
console.log(Object.assign({}, menu)) //line 67
delete newMenu.restId
console.log(Object.assign({}, menu)) //line 69
console.log(Object.assign({}, newMenu)) //line 70
this.restaurantListRef.child(`${menu.restId}/menuList`).push(newMenu)
})
}
在第 98 行,我从newMenu 中删除了restId,但它显然也从menu 中删除了restId,我希望它不会从menu 中删除restId因为它应该是 2 个不同的变量,对吧?我尝试使用var、let 或const,但结果还是一样。
请再指教。谢谢!
【问题讨论】:
标签: angular typescript ionic-framework