1

I have a backend API, which as shown in this POSTMAN screenshot is working. enter image description here

Here is my service:

getComments(line: string, department: string, startTime?: number, endTime?: number): Observable<Array<IComments>> {
    let headers = new Headers(
        {
            'Content-Type': 'application/x-www-form-urlencoded',
            'line': line,
            'cell': department,
            'start': new Date(),
            'end': ago(24, "hours")
        });
    let options = new RequestOptions({ headers: headers });
    return this.http.get('api/data/comments', options)
        .map(this.extractData)
        .catch(this.handleError);
}
private extractData(res: Response) {
    let body = res.json();
    return body.data || {};
}

and this is what my component contains:

  this.dataService.getComments('604', 'powders').forEach(result => {
      console.log('Res: ' + JSON.stringify(result));
      that.comments = result;
  });

However, all I get in the console is:

Res: {}

Why is the data not being returned, as it is in postman?

Update:

and if I console.log(body) in extractData, then I get:

Object {result: "COMMENTS604"}

5
  • 1
    And if you console.log in extractData? Commented Dec 10, 2016 at 15:10
  • @jonrsharpe I have updated my question with those details Commented Dec 10, 2016 at 15:16
  • 2
    Your screenshot shows: Content-Type: application/x-www-form-urlencoded. Your code uses 'Content-Type': 'application/json'. Don't send JSON to an endpoint not expecting JSON. Commented Dec 10, 2016 at 15:17
  • @JBNizet I am afraid that even when changing this to match my postman, I get an empty Res... Commented Dec 10, 2016 at 15:30
  • I misread your question. You're using a GET, so the Content-Type headre is irrelevant and should not even be present. You're sending new Date() instead of the string '080988' for your start header. And you're sending something unknown instead of '178798798' for the end header. The screen shot shows that you receive a JSON object as result, and your service returns an Observable, but you're calling forEach() on this observable as if it was an array, instead of subscribing to it. WHat you're doing in extractData doesn't make sense either, given that there is no data in the returned JSON. Commented Dec 10, 2016 at 15:44

1 Answer 1

1

In extractData you are returning body.data, but since the property is named result, I think you meant to return body.result

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.