angular - Angular:对象具有值,但属性未定义
问题描述
所以我从我的休息服务中获取数据,Dto 已填充,但是当我尝试获取属性时,它是未定义的。
我在这里获取数据
this.mapservice.getAllCoordinates().subscribe((data: CoordinateDto[]) => {
this.coordinates = data;
this.parseMapCoordinates();
this.initLocation();
});
在这里,我尝试将其解析为一个数字,这样我就可以用它填充我的另一个对象。但是属性都是未定义的。
parseMapCoordinates() {
let newCoords: MapCoordinates;
this.coordinates.forEach((coords) => {
console.log(coords);
console.log(coords.user_Id);
console.log(coords.date);
console.log(coords.koordinate);
newCoords.latitude = parseFloat(coords.koordinate.split(', ')[0]);
newCoords.longitude = parseFloat(coords.koordinate.split(', ')[1]);
this.mapCoordinates.push(newCoords);
});
}
当我将鼠标悬停在坐标上时,它会显示值,但是当我将鼠标悬停在坐标上时,它会显示“未定义”,我不知道为什么。
最后是 Dto
export interface CoordinateDto{
koordinate: string;
date: Date;
user_Id: number;
谢谢您的帮助 :)
解决方案
this.coordinates
是一个项目数组。所以你需要使用它的索引来访问它,例如:
parseMapCoordinates() {
let newCoords: MapCoordinates;
for (let i = 0; i < this.coordinates.length; i++) {
const coords = this.coordinates[i];
console.log(coords);
console.log(coords.user_Id);
console.log(coords.date);
console.log(coords.koordinate);
newCoords.latitude = parseFloat(coords.koordinate.split(', ')[0]);
newCoords.longitude = parseFloat(coords.koordinate.split(', ')[1]);
this.mapCoordinates.push(newCoords);
}
}
推荐阅读
- c++ - 使用 gcc-8.2 构建 cpprestsdk 时出错
- firebase-mlkit - FirebaseVisionImage / ML 工具包cropRect() 支持
- php - 如何在mysql中随机获取列值
- css - 如何将css应用于标签的属性?
- ms-access - Microsoft Access DDL 用于新的 bigint 字段
- html - 在内容左侧和右侧的空白空间(包装器的边距)被隐藏之前阻止包装器中的内容收缩
- django - 在 django-tables2 中显示误解
- linux - 如何在 Bash 中将字符串格式日期 yyyymmddThhmmss.sss 转换为日期?
- python - 使用 python 验证自定义邮件 ID
- mongodb - 使用对字段值的引用进行聚合中的 Mongodb 正则表达式