我是 Angular 的新手,有一个已经存在的项目要处理。我有一个用 Symfony 构建的 API 和用 Angular 构建的客户端。我有一个名为 Quotation 的模型。在获取路线报价/{id} 时,它通常可以工作,但出现此错误的某些报价除外:
GET http://api.local/api/quotations/1381 500 (Internal Server Error) core.js:12501 ERROR
HttpErrorResponse {headers: HttpHeaders, status: 500, statusText: "Internal Server Error", url: "http://api.local/api/quotations/1381", ok: false, …}
data: {data: {…}, notifications: Array(0)}
error: {data: {…}, notifications: Array(0)}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure response for http://api.local/api/quotations/1381: 500 Internal Server Error"
name: "HttpErrorResponse"
ok: false
repeatRequest: ƒ (newRequest)
request: HttpRequest {url: "http://api.local/api/quotations/1381", body: null, reportProgress: false, withCredentials: false, responseType: "json", …}
status: 500
statusText: "Internal Server Error"
url: "http://api.local/api/quotations/1381"
__proto__: HttpResponseBase
我找不到工作的人和不工作的人之间的区别。当我尝试使用 API 而不是客户端访问它时,出现此错误:
{
"data": {
"data": {
"@context": "/api/contexts/Error",
"@type": "hydra:Error",
"hydra:title": "An error occurred",
"hydra:description": "Notice: Trying to get property 'response' of non-object",
正如我所说的,我是 Angular 的新手,我不知道去哪里找,所以我会给你一些在客户端触发错误的代码。 quote.component.ts:
ngOnInit() {
this.route.paramMap
.pipe(
map(params => params.get('quotationId')),
withLatestFrom(this.route.queryParamMap),
switchMap(data => {
if (!!data[1].get('sync') && data[1].get('sync') === 'true') {
return this.quotationService.syncQuotation(+data[0]);
}
return this.quotationService.get(+data[0]);
}),
map(data => {
console.log(data);
if (!data['@id']) {
this.router.navigate(['/devis', +data.id]);
return data;
}
return data;
}),
withLatestFrom(this.store.pipe(select(selectQuotationState))),
switchMap(data => {
this.isFav = !!data[1].favorites.find(element => element.id === data[0].id);
this.quotation = data[0];
console.log(this.quotation);
return this.quotationService.getJobs(data[0]);
}),
switchMap((jobs: QuotationJob[]) => {
this.jobs = jobs;
return this.quotationService.search(this.quotation.clientName);
})
)
.subscribe((searchResults: Quotation[]) => {
this.searchResults = searchResults;
});
}
行“ return this.quotationService.get(+data[0]);”调用quote.service.ts:
get(id: number): Observable<Quotation> {
return this.rg.one('quotations', id).get();
}
例如,如果我询问 id 2,它会正常工作,我会得到我想要的数据,但是如果我询问 id 4,它会触发错误,当 id 2 和 4 都在数据库中时!我不知道该怎么办。
感谢您的帮助!