【问题标题】:undefined in TypeORM and NestJS在 TypeORM 和 NestJS 中未定义
【发布时间】:2022-01-17 02:36:09
【问题描述】:
 var name = await this.productRepo.findOne({where:{id}})['name']

大家好。我使用 typeorm 和 sqlite 从 ProductEntity 获取属性名称,但我得到的是 undefined

当我尝试运行 var name = await this.productRepo.findOne({where:{id}}) 时,我得到了类似的结果

ProductEntity {
    id: 1,
    name: 'فن لپتاپ',
    code: 'a57gr3f',
    quantity: 2,
    discription: 'ب',
    price: 3000000

} 我应该得到فن لپتاپ 而不是 undefined 如果您能提供帮助,我将不胜感激。

【问题讨论】:

    标签: sqlite nestjs typeorm


    【解决方案1】:
    await this.productRepo.findOne({where:{id}})['name']
    //    \____________________________________/
    //          this is an instance of Promise, not the resolved value
    //          and Promise doesn't have the property 'name'
    

    改为这样做:

    ( await this.productRepo.findOne({where:{id}}) )['name']
    

    【讨论】:

    • 感谢您的评论。我已经用var name = await this.productRepo.findOne({where:{id},select:['name']}).then((e)=>{ return e.name }) 代替了!
    猜你喜欢
    • 2020-09-03
    • 2021-02-27
    • 2020-04-19
    • 2020-12-09
    • 2021-08-05
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 2019-07-27
    相关资源
    最近更新 更多