I am trying to pass a response from my service to component so I can display it on the html page but I cant figure out how.
The component
uploadFile(): void {
this.errors = null;
let file = this.fileInput.nativeElement;
if (file.files && file.files[0]) {
let fileToUpload = file.files[0];
this.getInfoService
.uploadImage(fileToUpload)
.subscribe(
response => { console.log(response) },
err => {
//this.errors = err._body
this.errors = err.json().image
console.log("ERR", err.json().image)
},
() => console.log("()",'worked')
);
}
}
EDIT: Problem was in my service. Here is the working code.
uploadImage(fileToUpload: any) : Observable<any> {
let input = new FormData();
input.append("image", fileToUpload);
return this.http.post('http://Api.app/api/v1/upload', input)
.map(
(response: Response) => {
// I was missing this part
response.json()
}
);
}