【问题标题】:Object literal may only specify known properties - Does not exist in type 'SaveOptions'对象字面量只能指定已知属性 - 类型“SaveOptions”中不存在
【发布时间】:2020-11-27 04:59:39
【问题描述】:

我正在使用 graphQL Apollo、Postgres、Express.js 来构建一个小型网站。 我正在选择一个现有的profile,我想更新它的firstNamelastName

我的代码

  const profile = await Profile.findOne({where: { user: payload!.userId}}) 
  if (profile) {
    console.log(profile) <-- It returns correctly the profile I am looking for
    await profile.save({
      firstName, <-- Error here
      lastName
    })

但是,我收到以下错误:

'{ firstName: string; 类型的参数姓氏:字符串; }' 不可分配给“SaveOptions”类型的参数。 对象字面量只能指定已知属性,而“SaveOptions”类型中不存在“firstName”。ts(2345)

错误:https://imgur.com/a/Khdn6LH

我认为问题在于配置文件的 type,即 Profile |未定义。 其中 Profile 是从 typeORM 实体推断出来的:

@ObjectType()
@Entity("profile")
export class Profile extends BaseEntity{

    @Field(()=>String)
    @Column({ nullable: true }) 
    firstName: string;
    
    @Field(()=>String)
    @Column({ nullable: true }) 
    lastName: string;
}

【问题讨论】:

    标签: typescript typeorm


    【解决方案1】:

    快速浏览一下他们的文档,在我看来 save 不会将新数据作为输入。

    相反,他们直接修改对象(因此 profile.firstName = firstName),然后不带参数地调用 save。

    要保存的参数是用于配置保存方式的 SaveOptions,请参见此处:https://typeorm.delightful.studio/interfaces/_repository_saveoptions_.saveoptions.html

    所以 TypeScript 是说属性 firstName 在 SaveOptions 中不存在(这是真的),这就是您传递给需要有效 SaveOptions 对象的保存函数的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-24
      • 2018-09-06
      • 2021-02-18
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      相关资源
      最近更新 更多