0

I have an interface defining my data object as below:

  export interface Iemployee  {
    fileName: string;
    empData: IempData }
      export interface IempData {
    data: string;
  }

I was trying to retrieve a json object using observables .I could successfully load the object but unable to access any of the properties. I could see the object log in console ,but when I try to log the property I see undefined error.Also I could access properties during runtime in debug window but cant assign to a variable.Please help. Below is my service and component code.

Service Code:

downloadFile(): Observable<Iemployee > {       
    return this._http.get<Iemployee >(this._url)
        .map(response => {                
         return response as Iemployee;
        })
        .catch(this.handleError);
}

Component Code:

downLoadFile() {

    this._employeeService.downloadFile().subscribe(response => {       
       this.empData = response;
       console.log(response );// object logged successfully
       console.log(response.fileName); // undefined but can see it during debugging with a watch
       console.log(this.empData ); //can log 
       console.log(this.empData .fileName);// undefined

    }, Error => {
      console.log(Error);
    });
  }
ganesh310
  • 35
  • 6
  • What does `console.log(response)` print? `as IEda` doesn't add any properties. That's only a hint for the tools like auto-completion and compiler. – Günter Zöchbauer Oct 20 '17 at 11:04
  • Are you using `http` or `httpclient`? [Difference between HTTP and HTTPClient in angular 4?](https://stackoverflow.com/questions/45129790/difference-between-http-and-httpclient-in-angular-4). With `http` the body of the response can be found on property `data`. With `HttpClient` the body is returned directly in the resulting object. – Igor Oct 20 '17 at 11:04
  • Im really sorry Its a typo in my question. I was trying to cast to Iemployee type.Also I was trying to log this.empData in console. Im using HttpClient in my service.Also as a intial step Im loading data from a static json file. – ganesh310 Oct 20 '17 at 11:12
  • Console.log (response) is printing the object perfectly, but when I try to log the response.fileName its showing undefined – ganesh310 Oct 20 '17 at 11:19
  • `Console.log (response) is printing the object perfectly` <= I do not see that line in your code above, please add it as well as the output that is printed. `log the response.fileName its showing undefined` <= I also do not see this in your code above, please add it. – Igor Oct 20 '17 at 11:30
  • Im sorry im unable to edit the question but here is my component code : downLoadFile() { this._employeeService.downloadFile().subscribe(response => { this.empData = response; console.log(response); // Object logged console.log(response.fileName); // undefined error console.log(this.empData); //object logged but cant load properties }, Error => { console.log(Error); }); – ganesh310 Oct 20 '17 at 11:42
  • `Im sorry im unable to edit the question` <= You can always edit your own question. Use the [edit](https://stackoverflow.com/posts/46847777/edit) link underneath your question content and tags. – Igor Oct 20 '17 at 12:02
  • Sorry for too many comments .Since this is my first question ,I could not edit it .But the link worked. I edited the question .Please revisit – ganesh310 Oct 20 '17 at 12:17

0 Answers0