简体   繁体   中英

how to pass a response to parent component from service provider component of angular4 (ionic3), i got undefined value?

in parent component i got undefined value for data. here is my code.

parentComponent.ts

testMethod(){
  console.log('t', this.answer.postRequest(this.quetion).subscribe(data => { 
    console.log('d',data);
  })); //here i got undefined value to data
}

serviceProvider.ts

postRequest(ques:string):Observable<any>{
  let header = new HttpHeaders({
    'Content-Type' : 'application/json',"Accept":'application/json'
  });
  console.log(ques)

  return this.http.post(
    "http://localhost:8080/hello",
    {'q': ques},
    {headers: header}
  ).map(data => {
    console.log(data);
  }, // this work fine and value is assigned to data as expected
  error => {(error);// Error getting the data});
}

use like this

return this.http.post(" http://localhost:8080/hello ",{ 'q': ques },{ headers:header}) .map(data => { console.log(data); }) .map((resp: Response) => {return resp;});

it will helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM