【发布时间】:2021-03-25 12:52:32
【问题描述】:
前端 Angular 应用程序将 GET 发送到后端节点 API 服务器。虽然 API 成功完成了这个 GET,但它的响应 res.status(200).send(data); 不会在 NG 调用者处触发断点,但 Chrome F12 网络选项卡显示有数据返回。
---- NG前端组件.ts:
import { Component, NgModule, OnInit } from '@angular/core';
import { mySvc } from '../mySvc.service';
import { record } from '../models/record';
export class ListAllRecordsComponent implements OnInit {
private allRecords: record[] = [];
constructor(private mySvc: mySvc) {}
...
public getAllRecords()
{
this.mySvc.getAllRecords().subscribe(response => this.allRecords = response, );
}
}
---- NG mySvc.service.ts
import { record } from '../models/record';
@Injectable()
export class mySvc {
constructor(private http: HttpClient) { }
const url = 'http://localhost:3000/somewhere';
getAllRecords(): Observable<record[]>
{
return this.http.get(url)
.pipe(
map((data: any) => data.record), // ??? break point is not triggered as expected when API sends back 200.
catchError( error => { return throwError('bad')})
)
}
---- Node API 服务器控制器:
exports.pgGetAll = (req, res) =>
{
objPS.pgGetAll( (err, data) =>
{
if (err)
res.status(500).send(err.message);
else
{
res.status(200).send(data); // break point shows data has all records
}
});
}
---- objPS:
const pgGetAll = (cbFunc) =>
{
pool.query('select * from "schema1"."table1"')
.then
(
good =>
{
cbFunc(null, good) // break point shows good has all records
},
bad =>
{
cbFunc(bad)
}
);
}
【问题讨论】:
-
您能否展示如何使用服务中的 getAllRecords 方法?因为你应该在某处订阅 observable...
-
@bits 添加到帖子中,但如果可观察到没有得到任何东西,则与下游无关。
-
浏览器的 devtools
Network部分的响应是什么? -
需要调试。您在应用程序中有任何拦截器吗?你现在尝试了什么?如果您收到浏览器的响应 - 从问题中删除后端 sn-ps,这会令人困惑。
-
就在
mySvc中的地图操作员之前点击并尝试控制台记录数据对象。它可能会让您深入了解数据对象的结构。
标签: angular