0

Is it possible to post to url app/heroes/1/jobs

When I try to I get the error Cannot read property 'data' of null

createJob(hero: Hero, job: Job): Observable<Job> {
  const url = `${this.heroUrl}/${hero.id}/jobs`;
  return this.http.post(url, JSON.stringify(job), {headers: this.headers})
    .map(res => res.json().data)
    .catch(this.handleError);
}

2 Answers 2

2

It's currently not possible, without customizing it (which isn't really any easy task, but doable). What's happening is that the 1/jobs is taken to be the id of the heroes collection, and it will return null since the following is null

let heroes = [...]
let id = '1/jobs'
return heroes.find(hero => return hero.id === id);

Like I said, you can customize it, but it'll take some work. It would consist of overriding the parseUrl add adding a response interceptor or an interceptor for each HTTP method you want to override. You can see an example. It's explained somewhat in the docs.

I've been following the issues, and it seems like the maintainers pretty much feel this is more of a tool for demos and don't want to spend too much time trying to make this something that is robust and suitable for most cases. So they just make it customizeable, even though the customization is not really an easy task.

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

Comments

-1

Try this:

.map((res:Response) => res.json().data)

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.