Javascript 无法将值设置为Typescipt中的对象属性。无法设置属性';伊斯曼';未定义的
下面是类i初始化Photo类型的对象,该Photo是具有某些属性的接口。然后我尝试使用数组过滤器来过滤照片 过滤器返回照片数组的一个副本,但我指定了我想要的照片,因此我将有一个包含1个索引的数组。然后,我尝试将对象内部的一个属性设置为false,并出现“不能设置属性'ismain'的角度未定义”。你知道为什么吗 这是我的密码Javascript 无法将值设置为Typescipt中的对象属性。无法设置属性';伊斯曼';未定义的,javascript,arrays,json,Javascript,Arrays,Json,下面是类i初始化Photo类型的对象,该Photo是具有某些属性的接口。然后我尝试使用数组过滤器来过滤照片 过滤器返回照片数组的一个副本,但我指定了我想要的照片,因此我将有一个包含1个索引的数组。然后,我尝试将对象内部的一个属性设置为false,并出现“不能设置属性'ismain'的角度未定义”。你知道为什么吗 这是我的密码 setMainPhoto(photo: Photo) { this.userService .setMainPhoto(this.authService.decoded
setMainPhoto(photo: Photo) {
this.userService
.setMainPhoto(this.authService.decodedToken.nameid, photo.id)
.subscribe(
() => {
// We use the array filter method to filter the photos apart from the main photo
// Filter returns a copy of the photos array. Filters out anything that it doesn`t match in the p
this.currentMain = this.photos.filter(p => p.ismain === true)[0];
this.currentMain.ismain = false;
photo.ismain = true;
this.alertify.success('Successfully set to profile picture');
},
error => {
this.alertify.error('Photo could not be set as profile picture');
}
);
}
在上述方法中,会发生错误。
下面是我的照片界面
export interface Photo {
id: number;
url: string;
dateadded: Date;
ismain: boolean;
description: string;
}
错误
更新
问题已解决
来自服务器的响应需要与对象属性匹配。因此,我必须更改属性接口以匹配从服务器返回的JSON对象。试试这个
setMainPhoto(photo: Photo) {
this.userService
.setMainPhoto(this.authService.decodedToken.nameid, photo.id)
.subscribe(
() => {
// We use the array filter method to filter the photos apart from the main photo
// Filter returns a copy of the photos array. Filters out anything that it doesn`t match in the p
this.currentMain = this.photos.filter(p => p.ismain === true)[0];
if (this.currentMain) {
this.currentMain.ismain = false;
photo.ismain = true;
this.alertify.success('Successfully set to profile picture'); }
},
error => {
this.alertify.error('Photo could not be set as profile picture');
}
);
}
如果这样做有效,那么过滤器不会返回任何内容 试试这个
setMainPhoto(photo: Photo) {
this.userService
.setMainPhoto(this.authService.decodedToken.nameid, photo.id)
.subscribe(
() => {
// We use the array filter method to filter the photos apart from the main photo
// Filter returns a copy of the photos array. Filters out anything that it doesn`t match in the p
this.currentMain = this.photos.filter(p => p.ismain === true)[0];
if (this.currentMain) {
this.currentMain.ismain = false;
photo.ismain = true;
this.alertify.success('Successfully set to profile picture'); }
},
error => {
this.alertify.error('Photo could not be set as profile picture');
}
);
}
如果这样做有效,那么过滤器不会返回任何内容 有趣的是,我以前也有过这样的想法。Typescript对大小写的要求非常严格,如果您不完全匹配json对象,它将无法工作,创建一个新属性,等等
我发现在构建我的typescript模型之前查看json返回非常有用,以确保我与发送的对象匹配。有趣的是,我以前也有过这样的想法。Typescript对大小写的要求非常严格,如果您不完全匹配json对象,它将无法工作,创建一个新属性,等等
我发现在构建我的typescript模型之前查看json返回很有帮助,以确保我与发送的对象匹配。
this.currentMain=this.photos.filter(p=>p.ismain==true)[0]
-您确定之后在过滤的数组中有任何元素吗?有可能是这一行。currentMain
在这一行之后是未定义的
。下面是photo-editor.ts???@VLAZ的第115行中的哪个语句。来自服务器的JSON对象与接口参数this.currentMain=this.photos.filter(p=>p.ismain==true)[0]
-您确定之后在该过滤数组中有任何元素吗?有可能是这一行。currentMain
在这一行之后是未定义的
。下面是photo-editor.ts???@VLAZ的第115行中的哪个语句。来自服务器的JSON对象与接口参数Hi Thanasi不匹配-它正在提交为未定义。。。虽然在照片列表中我有4条记录,其中哪一条是ismain=true,但怎么可能不从照片列表中选择该记录呢?嗨,Thanasi-它是未定义的。。。虽然在照片列表中我有4条记录,其中哪一条是ismain=true,但怎么可能不从照片列表中选择该记录呢?同意..这样更好同意..这样更好