【问题标题】:How to fix 'Uncaught (in promise)' error in primeNG calendar如何修复primeNG日历中的“未捕获(承诺)”错误
【发布时间】:2019-05-09 10:26:10
【问题描述】:

我正在设置 primeNG 日历,想知道如何在编辑时显示保存的日期。

<p-calendar name="startDate" [(ngModel)]="classPrg.startDate"> </p-calendar> 
<p-calendar name="endDate" [(ngModel)]="classPrg.endDate"></p-calendar>
ngOnInit() { 
    this.classPrg = this.classPrgStore.state.entry.data; 
    if (!this.classPrg) { 
        let id: string = this.activedRoute.snapshot.params['id']; 
        if (await this.classPrgStore.loadClassPrg(id).toPromise()) { 
            this.classPrg = this.classPrgStore.state.entry.data; 
            this.classPrg.startDate = new Date(this.classPrg.startDate); 
            this.classPrg.endDate = new Date(this.classPrg.endDate); 
        }
    }
} 

【问题讨论】:

  • 你能展示一下你做出承诺的代码吗?
  • async ngOnInit() { this.classPrg = this.classPrgStore.state.entry.data; if (!this.classPrg) { let id: string = this.activedRoute.snapshot.params['id']; if (await this.classPrgStore.loadClassPrg(id).toPromise()) { this.classPrg = this.classPrgStore.state.entry.data; this.classPrg.startDate = new Date(this.classPrg.startDate); this.classPrg.endDate = new Date(this.classPrg.endDate); }}}

标签: angular primeng


【解决方案1】:

这个错误是警告你在一个promise中发生了一个错误但是没有处理程序。

由于您在承诺中使用async/await,因此您需要一个catch 块:

ngOnInit() { 
    this.classPrg = this.classPrgStore.state.entry.data; 
    if (!this.classPrg) { 
        let id: string = this.activedRoute.snapshot.params['id']; 
        try {
            if (await this.classPrgStore.loadClassPrg(id).toPromise()) { 
                this.classPrg = this.classPrgStore.state.entry.data; 
                this.classPrg.startDate = new Date(this.classPrg.startDate); 
                this.classPrg.endDate = new Date(this.classPrg.endDate); 
            }
        } catch (error) {
            // Handle error, or just leave blank to ignore error
        }
    }
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 2022-01-05
    • 2021-11-28
    • 1970-01-01
    • 2017-03-11
    • 1970-01-01
    • 2022-12-19
    相关资源
    最近更新 更多