【发布时间】:2019-05-08 18:08:45
【问题描述】:
我想在 es6 模型中设置 JSON API,但出现此错误 TypeError:无法读取未定义的属性“first_name”
JSON API:
{ “类型”:“用户”, “身份证”:2, “属性”: { “用户名”:“madelynn81”, “电子邮件”:“taylor63@mills.biz”, "first_name": "埃米尔", "last_name": "Veum", “created_at”:“2018-11-17 11:48:13” }, “链接”:{ “自我”:“http://test.test/api/v1/user/2” } }es6 型号
class UserModel {
constructor(data) {
this.id = data.id;
this.first_name = data.attributes.first_name;
this.last_name = data.attributes.last_name;
this.username = data.attributes.username;
this.email = data.attributes.email;
this.created_at = data.attributes.created_at;
this.link = data.links.self;
}
get getId() {
return this.id;
}
set setId(value) {
this.id = value;
}
get getFirstName() {
return this.first_name;
}
set setFirstName(value) {
this.first_name = value;
}
get getLastName() {
return this.last_name;
}
set setLastName(value) {
this.last_name = value;
}
get getUsername() {
return this.username;
}
set setUsername(value) {
this.username = value;
}
get getEmail() {
return this.email;
}
set setEmail(value) {
this.email = value;
}
get getCreatedAt() {
return this.created_at;
}
set setCreatedAt(value) {
this.created_at = value;
}
get getLink() {
return this.link
}
set setLink(value) {
this.link = value
}
}
我该如何解决这个问题
【问题讨论】:
标签: javascript json ecmascript-6