私は私のオブジェクトをサーバーレスポンスから返しました。これはgoogle chrome開発ツールのネットワークタブに表示されます。node-expressサーバーの応答を見ることができますが、私はそれを私のangular2 promiseサービスで使用できません。
module.exports = (req, res) => {
var obj = {
name: "Thabo",
age: 23
};
res.json({obj})
};
そして私は、私も「し、」約束の上の応答を記録し、それをログに記録し
export class MyService {
constructor(private http: Http) { }
getMessage(): Promise<any> {
return this.http.get('/map-moving-agents')
.toPromise()
.then((res)=> {
console.log(res.json().data);
res.json().data;
})
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
サーバーから観察可能な応答を取得するために約束を使用していますangular2-typescriptですサービスを持っています未定義。
そして、これが
export class MapMovingAgents implements OnInit{
msg: {};
constructor(private myService: MyService){}
getMessage(): void {
this.myService.getMessage().then((res) => { this.msg = res;})
console.log(this.msg);
}
ngOnInit(): void {
this.getMessage();
}
}
res.json().obj
undefined
帰りでウィルdata
フィールドを持っていませんか? – echonaxは、私はちょうど追加したIVEはIVEはそれを試して助けない – Teebo
サイモン? return文を 'res.json()。data'と' console.log'に追加します。 – echonax