【发布时间】:2019-11-03 01:57:20
【问题描述】:
我在我的一种方法中遇到了一种奇怪的行为。它打印entity to user
和start loop 但在那之后什么也没有发生。它以某种方式卡住了,没有任何错误!?它从未达到end loop,当然也没有达到finished
我真的不知道它有什么问题。我正在使用类似的东西从一种类型转换为另一种类型,但我在这里遗漏了一些东西。
方法:
entityToClientUser(users: any) {
const clientUsers = [];
console.log('entity to user');
console.log(users);
for (const u of users) {
console.log('start loop');
clientUsers.push(
new ClientUser(
u.id,
u.username,
u.email,
'',
'',
u.description,
false,
u.registrationDate,
new Date(),
u.role.role ? u.role.role : 1
)
);
console.log('end loop');
}
console.log('finished');
console.log(clientUsers);
return clientUsers;
}
给方法的对象:
[ {
id: 1,
username: 'test',
email: 'info@example.com',
password: 'b234234c34899hwerwer4535rfa10a666edgfh43',
avatar: null,
ip: null,
description: 'A simple user.',
isGuest: null,
lastAuth: null,
registrationDate: 2019-10-31T23:56:14.170Z
}
]
ClientUser 类
export class ClientUser {
id: number;
username: string;
email: string;
avatar: string;
ip: string;
description: string;
isGuest: boolean;
lastAuth: Date;
role: number;
registrationDate: Date;
constructor(id: number, username: string, email: string, avatar: string, ip: string, description: string, isGuest: boolean, registrationDate: Date, lastAuth: Date, role: number) {
this.id = id;
this.username = username;
this.email = email;
this.avatar = avatar;
this.ip = ip;
this.description = description;
this.isGuest = isGuest;
this.registrationDate = registrationDate;
this.lastAuth = lastAuth;
this.role = role;
}
}
【问题讨论】:
-
我知道您说没有引发错误,但
u.role.role似乎可能会引发未定义的错误,因为角色未在用户对象中定义。是这样吗? -
是的 glneto 是写的,应该是 u.role 而不是 u.role.role
-
是的,这就是原因!但我不明白为什么我在控制台中没有收到任何错误。谢谢!
标签: javascript typescript nestjs