Angular 打字稿对象 属性 显然不是未定义但 属性 是
Angular Typescript object property is clearly not undefined but property is
我遇到了一个相当荒谬的问题。
来自订阅的可观察对象的对象在打印到控制台时清楚地包含数据,但在实际的打字稿代码中,表示其未定义。
自己看看:
initData(): void {
this.backendService.getData().subscribe((depotDays: DepotDayAcc[]) => {
console.log(depotDays)
console.log(depotDays[0])
console.log(depotDays[0].investment)
console.log(depotDays[0]["day"])
console.log('depotDays[0] == undefined', depotDays[0] == undefined)
console.log('depotDays[0].investment == undefined', depotDays[0].investment == undefined)
})
}
constructor(
public day: Date,
public investment: number = -1,
public profit: number = 0,
public value: number = 0,
public percent: number = 0,
public tickers: Set<string> = new Set<string>()) {
}
正如 ShamPooSham 在他的评论中指出的那样,您的数据都是字符串。你有一个字符串数组,而不是对象。
const depotDay = JSON.parse(depotDays[0]);
console.log(depotDay.investment);
选择要么修复有效负载,使其正确反序列化,要么自行反序列化每个条目。
我遇到了一个相当荒谬的问题。
来自订阅的可观察对象的对象在打印到控制台时清楚地包含数据,但在实际的打字稿代码中,表示其未定义。
自己看看:
initData(): void {
this.backendService.getData().subscribe((depotDays: DepotDayAcc[]) => {
console.log(depotDays)
console.log(depotDays[0])
console.log(depotDays[0].investment)
console.log(depotDays[0]["day"])
console.log('depotDays[0] == undefined', depotDays[0] == undefined)
console.log('depotDays[0].investment == undefined', depotDays[0].investment == undefined)
})
}
constructor(
public day: Date,
public investment: number = -1,
public profit: number = 0,
public value: number = 0,
public percent: number = 0,
public tickers: Set<string> = new Set<string>()) {
}
正如 ShamPooSham 在他的评论中指出的那样,您的数据都是字符串。你有一个字符串数组,而不是对象。
const depotDay = JSON.parse(depotDays[0]);
console.log(depotDay.investment);
选择要么修复有效负载,使其正确反序列化,要么自行反序列化每个条目。