I have a function in an angular2 service, which is passed an id to append to a base URL for a Http get request however it doesn't work. When i put in the exact string it gets the data fine, but it won't let me append the variable like
'http://localhost:3000/project/allTask/' + project_id
getTasks(project_id){
this.loadToken();
let headers = new Headers();
headers.append('Content-Type','application/json');
return this.http.get('http://localhost:3000/project/allTask/5b0919ea256f2e15f8f4364f' , {headers: headers})
.map(res => res.json());
}
Update:
I believe the project_id when passed in undefined. This is a function of my component.
ngOnInit() {
this.authService.getProfile().subscribe(profile => {
this.user_id = profile.user._id;
console.log(this.user_id); //RETURNS CORRECT ID HERE
},
err =>{
console.log(err);
return false;
});
console.log(this.user_id);//UNDEFINED
this.authService.getProjects(this.user_id).subscribe(data => {
this.projects = data;
},
err =>{
console.log(err);
return false;
});
}
project_idcontains the correct string? Try to log it to the console to check.