Javascript 将对象推入数组的Typescript被卡住,没有任何错误
在我的一种方法中,我面临着一种奇怪的行为。它向用户打印Javascript 将对象推入数组的Typescript被卡住,没有任何错误,javascript,typescript,nestjs,Javascript,Typescript,Nestjs,在我的一种方法中,我面临着一种奇怪的行为。它向用户打印实体 和开始循环,但之后什么都不会发生。它不知怎么卡住了,没有任何错误!?它从未到达结束循环,当然也没有完成 我真的不知道它出了什么问题。我正在使用类似的东西从一种类型转换到另一种类型,但我缺少一些东西 方法: entityToClientUser(users: any) { const clientUsers = []; console.log('entity to user'); console.log(user
实体
和开始循环
,但之后什么都不会发生。它不知怎么卡住了,没有任何错误!?它从未到达结束循环
,当然也没有完成
我真的不知道它出了什么问题。我正在使用类似的东西从一种类型转换到另一种类型,但我缺少一些东西
方法:
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
}
]
对象指定给该方法:
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;
}
}
这是因为您将用户定义为任意:
users:any
Typescript不会就其中一个用户对象上引用的任何内容向您发出警告
您需要将其更改为users:ClientUser[]
或Typescript可以计算的其他形状
对于您的情况,我建议您:
ClientUserBase.ts
export class ClientUserBase {
id: number;
username: string;
email: string;
description: string;
registrationDate: Date;
role: number;
}
ClientUser.ts
//Inherits common properties from ClientUserBase
export class ClientUser extends ClientUserBase {
avatar: string;
ip: string;
isGuest: boolean;
lastAuth: Date;
constructor(clientUserBase: ClientUserBase, avatar: string, ip: string, isGuest: boolean, lastAuth: Date) {
this.id = clientUserBase.id;
this.username = clientUserBase.username;
this.email = clientUserBase.email;
this.avatar = avatar;
this.ip = ip;
this.description = clientUserBase.description;
this.isGuest = isGuest;
this.registrationDate = clientUserBase.registrationDate;
this.lastAuth = lastAuth;
this.role = clientUserBase.role;
}
要执行的方法:
entityToClientUser(users: ClientUserBase[]) {
const clientUsers: ClientUser[] = [];
console.log('entity to user');
console.log(users);
for (const u of users) {
console.log('start loop');
clientUsers.push(new ClientUser(u, '', '', false, new Date()));
console.log('end loop');
}
console.log('finished');
console.log(clientUsers);
return clientUsers;
}
反正是这样的…我知道您说过不会抛出错误,但是
u.role.role
似乎会抛出未定义的错误,因为用户对象中没有定义role。是这样吗?是的glneto写的,应该是u.role而不是u.role.roleYes这就是原因!但是我不明白为什么我没有在控制台中得到任何错误。非常感谢。