I would like to map a model with nested JSON, but it is not recognizing 'message' id.
I've searched a lot and can not find what's wrong.
Would also like to create an auto-map data comparing JSON with the model (without need to set each value), is that possible?
JSON DATA:
{
"author": "John Doe",
"message": {
"id": 32133,
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
}
}
}
post.model.ts
export class PostModel {
author: string;
message: PostMessageModel;
constructor() { }
convert(data: any) {
this.author = data.author;
this.message.id = data.message.id; // ERROR HERE
this.message.text = data.message.text;
...
}
}
interface PostMessageModel {
id: number;
text: string;
}
Error:
ERROR TypeError: Cannot set property 'id' of undefined
at PostModel.webpackJsonp.343.PostModel.convert (post.model.ts:10)
...