【问题标题】:Typescript pushing object into array gets stuck without any error打字稿将对象推入数组卡住而没有任何错误
【发布时间】:2019-11-03 01:57:20
【问题描述】:

我在我的一种方法中遇到了一种奇怪的行为。它打印entity to userstart 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


【解决方案1】:

这是因为您将用户定义为 any: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;
}

反正就是这样……

【讨论】:

    猜你喜欢
    • 2018-09-12
    • 2020-06-26
    • 2016-03-07
    • 1970-01-01
    • 2022-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多