提问人:btror 提问时间:4/30/2023 更新时间:4/30/2023 访问量:26
为什么我无法访问打字稿中加载的对象数组中对象的属性?
Why can't I access a property of an object in a loaded array of objects in typescript?
问:
所以,我不能把我的思想放在这个问题上......
基本上,我在类中有一些变量和一个构造函数:
currentUser: UserModel | null = null;
currentServer: ServerModel | null = null;
serverMembers: UserModel[] = [];
constructor(
private modalService: MdbModalService,
private userDataProvider: UserDataProvider,
private serverDataProvider: ServerDataProvider
) {
this.currentUser = this.userDataProvider.getCurrentUser();
this.serverDataProvider
.loadServerMembers()
.pipe(
mergeMap((members) => from(members)),
mergeMap((member) => this.userDataProvider.getUserById(member.id)),
toArray(),
tap((resolvedMembers) => {
console.log(resolvedMembers);
console.log(resolvedMembers[0]);
console.log(resolvedMembers[0].userName);
this.serverMembers = resolvedMembers;
})
)
.subscribe();
}
控制台日志的输出分别为:
// console log 1
0: {id: 1, userName: 'Jeff'}
1: {id: 2, userName: 'John'}
// console log 2
{id: 1, userName: 'Jeff'}
// console log 3
undefined
问题是第三个控制台日志似乎无法读取对象属性值,即使 userName 有一个值。我的问题是到底是什么,为什么???
答: 暂无答案
评论
console.log(Object.keys(resolvedMembers[0])[1] === 'userName')
false
console.log(Object.keys(resolvedMembers[0])[1] )