后端 Nest JS 服務代碼:
async show(): Promise<any> {
var response: dto = new dto();
var data = await this.sequelize.query('Exec API_Select',
{
type: sequelize.QueryTypes.SELECT
});
if(data.length>0){
data.map((item:any) => {
response.foo1=item.foo1;
response.foo2=item.foo2;
response.foo3=item.foo3;
});
return response;
}
}
前端 Angular 代碼(服務):
getdata(){
const headers = new HttpHeaders().set('Content-Type', 'application/json').set('Authorization','Bearer' ' ' token);
this.http.post<interface1>('http://localhost:3000/getdata/',null,{headers})
.pipe(map(data => ({
foo1:data.foo1,
foo2:data.foo2,
foo3:data.foo3
})))
.subscribe(foo => console.log(foo))
}
現在,如果我在 eg 中提供虛擬值,data.foo1那么它可以作業(這意味著 Angular 中的服務和組件代碼很好。后端也可以正常作業,因為我可以在 Postman 中獲取資料。唯一的問題是將資料從后端映射到前端.
我已經嘗試過.subscribe,.map但它不起作用。我也試過了,<interface1>但結果還是一樣。
我是這個領域的初學者,并且是 Nest JS 和 Angular 的新手。請看一看。謝謝你。
編輯:
return this.http.post('http://localhost:3000/getdata/',null,{headers}).subscribe(data => {
this.dataout = data});
如果我將值作為單個變數(例如在上面的代碼中data)獲取,并且在后端有一個變數data。那么它也可以正常作業。唯一的問題是映射。
來自 Postman 的 API 回應:
{
"statusCode": 200,
"isSuccess": true,
"message": "ok",
"data": [
{
"foo1": 3,
"foo2": "abc",
"foo3": "def",
},
{
"foo1": 1,
"foo2": "ghi",
"foo3": "jkl",
}
]
}
uj5u.com熱心網友回復:
使用正確的回復語法
this.http
.post<interface1>('http://localhost:3000/getdata/',null,{headers})
.pipe(map(({ data }) => ({
foo1:data.foo1,
foo2:data.foo2,
foo3:data.foo3
})))
.subscribe(foo => console.log(foo))
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/462675.html
上一篇:如何以角度測驗帶有主題的服務
