Angular 6/Typescript HTTP post 请求 returns 当服务从另一个服务调用时未定义
Angular 6/Typescript HTTP post request returns undefined when service called from another service
我正在使用 Angular 6.
我正在从另一个服务(服务 1)调用服务(服务 2)getData
。 post 请求在service2中成功,将数据打印到控制台。但是,数据不会返回到调用服务 2 的服务 1,并且 'result' 对象始终未定义。
正在调用服务 (service1)
this.service.getData(id, token).then(
result => {
console.log("result " + result);
},
error => {
console.log("error " + error);
});
服务(服务2)
getData(id, token): Promise < any > {
var startTime: number = new Date().getTime();
return new Promise < any > ((resolve, reject) => {
this.http.post(url, soapMessage, {
headers: new HttpHeaders().set('Content-Type', 'text/xml'),
responseType: 'text'
}).toPromise().then(
res => { // Success
resolve();
console.log(res);
//return res;
},
msg => { // Error
}
);
});
}
您错过了通过 res
的解析:
.then(res => { // Success
resolve(res);
console.log(res);
//return res;
},
注意:
不太确定您为什么无缘无故地返回 Promise。您的代码可以像这样进行显着重构:
服务 2:
getData(id, token): Promise < any > {
var startTime: number = new Date().getTime();
return this.http.post(url, soapMessage, {
headers: new HttpHeaders().set('Content-Type', 'text/xml'),
responseType: 'text'
});
}
服务 1:
this.service.getData(id, token).subscribe(
result => {
console.log("result " + result);
},
error => {
console.log("error " + error);
});
最好使用 rxjs observable。
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {Observable} from 'rxjs';
import {Idata} from './Idata'
@Injectable()
export class ShareService {
constructor(private httpc:HttpClient)
{
}
public getPosts():Observable<Idata[]>
{
return this.httpc.get<Idata[]>('https://jsonplaceholder.typicode.com/posts');
}
}
//In your component subscribe to Observable<Idata[]> to get instance of Idata
public data:Array<Idata>=[];
constructor(public service:ShareService)
{
this.service.getPosts().subscribe(value => {
this.data=value;
});
}
我正在使用 Angular 6.
我正在从另一个服务(服务 1)调用服务(服务 2)getData
。 post 请求在service2中成功,将数据打印到控制台。但是,数据不会返回到调用服务 2 的服务 1,并且 'result' 对象始终未定义。
正在调用服务 (service1)
this.service.getData(id, token).then(
result => {
console.log("result " + result);
},
error => {
console.log("error " + error);
});
服务(服务2)
getData(id, token): Promise < any > {
var startTime: number = new Date().getTime();
return new Promise < any > ((resolve, reject) => {
this.http.post(url, soapMessage, {
headers: new HttpHeaders().set('Content-Type', 'text/xml'),
responseType: 'text'
}).toPromise().then(
res => { // Success
resolve();
console.log(res);
//return res;
},
msg => { // Error
}
);
});
}
您错过了通过 res
的解析:
.then(res => { // Success
resolve(res);
console.log(res);
//return res;
},
注意:
不太确定您为什么无缘无故地返回 Promise。您的代码可以像这样进行显着重构:
服务 2:
getData(id, token): Promise < any > {
var startTime: number = new Date().getTime();
return this.http.post(url, soapMessage, {
headers: new HttpHeaders().set('Content-Type', 'text/xml'),
responseType: 'text'
});
}
服务 1:
this.service.getData(id, token).subscribe(
result => {
console.log("result " + result);
},
error => {
console.log("error " + error);
});
最好使用 rxjs observable。
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {Observable} from 'rxjs';
import {Idata} from './Idata'
@Injectable()
export class ShareService {
constructor(private httpc:HttpClient)
{
}
public getPosts():Observable<Idata[]>
{
return this.httpc.get<Idata[]>('https://jsonplaceholder.typicode.com/posts');
}
}
//In your component subscribe to Observable<Idata[]> to get instance of Idata
public data:Array<Idata>=[];
constructor(public service:ShareService)
{
this.service.getPosts().subscribe(value => {
this.data=value;
});
}