简体   繁体   English

hasOwnProperty 总是使用 Typescript 返回 false

[英]hasOwnProperty always returns false using Typescript

I'm implementing a filter in TypeGraphQL and I'm first checking to see if the Class has the property requested by the user to filter.我正在 TypeGraphQL 中实现一个过滤器,我首先检查该类是否具有用户请求过滤的属性。 I'm trying to use hasOwnProperty but it always returns false even though the property is correct.我正在尝试使用 hasOwnProperty 但它总是返回 false 即使该属性是正确的。

Query within Resolver:在解析器中查询:

@Query(() => [Dog] )
  async dogs(
    @Arg('filter') filter: FilterInput
  ): Promise<Dog []> {
    console.log(filter.field)
    console.log(Dog.prototype.hasOwnProperty(filter.field))

    const dogs = await Dog.find({where: {[filter.field]: `${filter.value}`}})

    return dogs
  }

Dog Entity:狗实体:

@ObjectType()
@Entity()
export class Dog extends BaseEntity {
  @Field(() => ID)
  @PrimaryGeneratedColumn()
  id: number

  @Field()
  @Column()
  name: string

  @Field()
  info(@Root() parent: Dog): string {
    return `gender: ${parent.gender} ; name: ${parent.name}`
  }

  @Field()
  @Column()
  gender: string
}

FilterInput Type:过滤器输入类型:

import { InputType, Field } from "type-graphql";


@InputType()
export class FilterInput {
  @Field()
  field: string

  @Field()
  value: string
}

I was able to solve this with an enum and just compare the input string with the enum values enum:我能够用枚举解决这个问题,只需将输入字符串与枚举值进行比较:

export enum dogFields {
  name = 'name',
  gender = 'gender'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM