I have this Angular 4 service that makes a HTTP request and returns the data. However, when I log the result in console I get undefined
.
This is my service:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class MenuService {
constructor(private http: HttpClient) {
}
menu: any;
getMenu(){
this.http.get('/assets/MENU.json').subscribe(data => {
// Read the result field from the JSON response.
let newValue = JSON.stringify(data).replace('{"Node":', '[');
newValue = newValue.substring(0,newValue.length - 1);
newValue+="]";
this.menu=JSON.parse(newValue);
});
console.log(this.menu);
return this.menu;
}
}