【问题标题】:How to convert promise <any> to promise <entity>如何将 promise <any> 转换为 promise <entity>
【发布时间】:2022-01-07 12:49:29
【问题描述】:

我是这个打字稿的新手,nestjs。我想知道如何将Promise&lt;any[]&gt; 转换为Promise&lt;MyEntity[]&gt; 以便成功执行我创建的这段代码:

 const usersfromTransaction = this.repoTransaction
      .createQueryBuilder()
      .select('user_id', 'id')
      .distinctOn(['user_id'])
      //.leftJoinAndSelect("user", "user")
      .where('EXTRACT(YEAR FROM created_date)=EXTRACT(YEAR FROM NOW())')
      .andWhere('extract(month from created_Date)=extract(month from now())')
      .limit(10)
      .getRawMany();

    const users = this.repoUser.find(usersfromTransaction);

    return user;

用户实体:

@Entity()
export class User {
@PrimaryGeneratedColumn('uuid')
id:string;
 @Column({ nullable: true })
firstname:string;
 @Column({ nullable: true })
lastname:string;
}

这行代码有错误this.repoUser.find(usersfromTransaction);

【问题讨论】:

  • repoUser的TS类型是什么?
  • @MicaelLevi,先生更新了这个问题。
  • 您没有显示this.repoUser 的来源。如果您使用了custom typeorm repository,则users 的类型将为Promise&lt;User[]&gt;。先试试吧

标签: node.js typescript promise nestjs typeorm


【解决方案1】:

您必须先将其转换为unknownany,就像这样...

const usersfromTransaction = ......... as unknown as Promise<MyEntity[]>;

【讨论】:

    猜你喜欢
    • 2019-07-25
    • 2018-11-04
    • 2017-04-04
    • 1970-01-01
    • 2015-12-11
    • 2017-01-12
    • 2016-07-05
    • 1970-01-01
    相关资源
    最近更新 更多