xameeramir xameeramir - 23 days ago 6
Javascript Question

Why is request-promise not returning the results to postman and angular 2's map?

We have an Angular 4 service with a function like this:

callPostAPI() {

var headers = new Headers();
headers.append('Content-Type', 'application/json');

let options = {}; //passing no options

return this.http.post('http://localhost:9000/v1/api/testapi', options, {
headers: headers
})
.map((response: Response) => {

return response.json();
})
.catch((error: Response) => {
return Observable.throw(error || 'server error');
});
}


The rp function is like this:

app.post("/v1/api/testapi", (req, res) => {

rp({
method: 'GET',
url: 'http://maps.googleapis.com/maps/api/geocode/json',
qs: {
address: '66309'
}
})
.then(res => {
console.log(res); //proper results received
return res;
}).catch(err => {
return err;
});
}


However, this is not returning any response when called either from postman or through Angular 4 service function.

The results are received and we can see them on the
console
as well. All this is working without headers.


Why is
request-promise
not returning the results?

Answer Source

You never handle the result of request promise on your server side. I'm assuming you are using express, try this:

app.post("/v1/api/testapi", (req, res, next) => {

    rp({
      method: 'GET',
      url: 'http://maps.googleapis.com/maps/api/geocode/json',
      qs: {
        address: '66309'
      }
    })
      .then(result => {
        console.log(res); //proper results received
        return res.status(200).json(result);
    }).catch(next);
}