Magofoco
Magofoco

Reputation: 5466

Object literal may only specify known properties - Does not exist in type 'SaveOptions'

I am using graphQL Apollo, Postgres, Express.js to build a small website. I am selecting an existing profile and I would like to update its firstName and lastName

My code

  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
    })

However, I get the following error:

Argument of type '{ firstName: string; lastName: string; }' is not assignable to parameter of type 'SaveOptions'. Object literal may only specify known properties, and 'firstName' does not exist in type 'SaveOptions'.ts(2345)

Error: https://i.sstatic.net/W7BEd.jpg

I think the problem is with the type of profile which is Profile | undefined. Where Profile is inferred from the typeORM entity:

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

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

Upvotes: 1

Views: 6350

Answers (1)

Milton
Milton

Reputation: 1012

From a quick glance at their docs it seems to me like save doesn't take the new data as input.

Rather, they modify the object directly (so profile.firstName = firstName) and then call save without arguments.

The argument to save is for the SaveOptions which configure how the save should be done, see here: https://typeorm.delightful.studio/interfaces/_repository_saveoptions_.saveoptions.html

So TypeScript is saying that the property firstName doesn't exist in SaveOptions (which is true) and that's what you're passing in to the save function which expects a valid SaveOptions object.

Upvotes: 0

Related Questions