Cannot read property 'title' of undefined, but title is displayed

Viewed 21651

I'm new with angular 4 , and I'm trying to build my first application.

The title is displayed in the screen but get this error in the console :

enter image description here

This is line 26 in the html file:

<h1>{{result.title}}</h1>

This is TravelDetailComponent:

export class TravelDetailComponent implements OnInit {

result: string[];
id: number;

constructor(private http: Http, private globals: Globals, private activatedRoute: ActivatedRoute) {
}


ngOnInit() {
  this.activatedRoute.params.subscribe((params: Params) => {
      this.id = params['id'];
      console.log(this.id);
  });
  this.http.get(this.globals.baseUrl + 'travels/' + this.id)
      .map(res => res.json()).subscribe(result => this.result = 
result);
}

}

why the title is displayed in the screen but I get this error in the console and how can I solve it ?

3 Answers

You have probably not called the body-parser middleware. Add this to your code below the app initialization.

// body-parser

app.use(express.json())

app.use(express.urlencoded({
    extended: true
}));
Related