Yar*_*rdi 5 api asynchronous promise typescript angular
我的标题中出现了我一生中的第一次错误:未处理的承诺拒绝:错误:收到消息的错误响应。我不知道是什么导致了这个错误,但幸运的是我的应用程序中当前只有一个 api 调用。我打算添加一些 catch 或promise.all,但我不知道在哪里以及每次尝试都失败了。有我的代码:
组件.ts:
async ngOnInit(): Promise<void> {
await this.attractionService.getAttractions().then((results) => {
results.forEach((attraction: { id: number; name: string; description: string; type: string; distance: string | undefined; imagePath: string | undefined; }) => {
this.section2ndModelAttractions.push(
new Card(
attraction.id,
attraction.name,
attraction.description,
attraction.type,
attraction.distance,
attraction.imagePath
)
)
})
})
}
Run Code Online (Sandbox Code Playgroud)
还有我的景点服务:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { firstValueFrom } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AttractionService {
constructor(private http: HttpClient) { }
async getAttractions(): Promise<any> {
return firstValueFrom(this.http.get<any>("https://localhost:7273/api/Attraction/All"));
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我应该如何处理我的代码来修复该错误?或者也许你们需要更多详细信息来修复它,我会提供您修复该问题所需的内容。
#编辑1
我评论了上面的所有代码并删除了 ngOnInit 上的异步,但仍然有这个错误。我还在下面的编辑中发布了整个错误。我不知道是什么原因导致该错误。如何找到它的源头呢?
错误:
Unhandled Promise Rejection: Error: Error response received for message <get-frame-manager-configuration>
PromiseEmptyOnRejected
PromiseReactionJob
Run Code Online (Sandbox Code Playgroud)
#编辑2
我发现了一些重要的东西!错误仅出现在 macOS 上的 safari 浏览器中。