gpt4 book ai didi

javascript - 正确使用 Promise.resolve().then() 错误 : Uncaught (in promise): TypeError: Cannot read property

转载 作者:行者123 更新时间:2023-12-01 00:16:44 24 4
gpt4 key购买 nike

我有这个代码:


export class Page {
constructor(public modalController: ModalController) { }

private async openModal(value) {
const modal = await this.modalController.create({
component: AnotherPage,
componentProps: {
"paramID": 123,
"paramTitle": "Test Title",
"takenPhoto": "null"
}
});

return await modal.present();
}

public async takePicture() {
this.openModel(null); // here's OK
Promise.resolve(this.someMethod()).then(this.openModal); // ERROR!!
}

}

关于方法takePicture我想在执行 Promise.resolve 后打开一个模式。第一次执行时一切正常,但第二次执行时出现错误。

错误:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'modalController' of undefined
TypeError: Cannot read property 'modalController' of undefined
at tab2.page.ts:25
at Generator.next (<anonymous>)
at tslib.es6.js:73
at new ZoneAwarePromise (zone-evergreen.js:876)
at Module.__awaiter (tslib.es6.js:69)
at openModal (tab2.page.ts:22)
at ZoneDelegate.invoke (zone-evergreen.js:359)
at Object.onInvoke (core.js:34201)
at ZoneDelegate.invoke (zone-evergreen.js:358)
at Zone.run (zone-evergreen.js:124)
at resolvePromise (zone-evergreen.js:797)
at resolvePromise (zone-evergreen.js:754)
at zone-evergreen.js:858
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Object.onInvokeTask (core.js:34182)
at ZoneDelegate.invokeTask (zone-evergreen.js:390)
at Zone.runTask (zone-evergreen.js:168)
at drainMicroTaskQueue (zone-evergreen.js:559)

最佳答案

export class Page {
constructor(public modalController: ModalController) { }

private async openModal(value) {
const modal = await this.modalController.create({
component: AnotherPage,
componentProps: {
"paramID": 123,
"paramTitle": "Test Title",
"takenPhoto": "null"
}
});

return modal.present();
}

public async takePicture() {
await this.openModel(null); // Need to await a promise
const data = await this.someMethod()
await this.openModal(data);
}

}

关于javascript - 正确使用 Promise.resolve().then() 错误 : Uncaught (in promise): TypeError: Cannot read property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59709705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com