Home > Net >  Type 'Document<any, any, any>' is missing the following properties from type 'I
Type 'Document<any, any, any>' is missing the following properties from type 'I

Time:09-25

I am not crystal clear on what this error is saying:

Type 'Document<any, any, any>' is missing the following properties from type 'IAccount': _type, pai

47 return newAccount;

The error is in regards to this method:

async createAccount(
    dbSession: ClientSession,
    pai: string
  ): Promise<IAccount> {
    const newAccount = new this.Model(
      new (class implements IAccount {
        _id = mongoose.Types.ObjectId();
        _type: "Account" = "Account";
        pai = pai;
      })()
    );
    await newAccount.save(dbSession);
    return newAccount;
  }

I am also getting a related error that says:

Overload 1 of 3, '(options?: SaveOptions | undefined): Promise<Document<any, any, any>>', gave the following error. Type 'ClientSession' has no properties in common with type 'SaveOptions'. Overload 2 of 3, '(options?: SaveOptions | undefined, fn?: Callback<Document<any, any, any>> | undefined): void', gave the following error. Type 'ClientSession' has no properties in common with type 'SaveOptions'. Overload 3 of 3, '(fn?: Callback<Document<any, any, any>> | undefined): void', gave the following error. Argument of type 'ClientSession' is not assignable to parameter of type 'Callback<Document<any, any, any>>'.

46 await newAccount.save(dbSession);

But it seems to be related to AccountSchema model which has an any in its angle brackets:

interface IAccount {
  _id: ID;
  _type: "Account";

  pai: string;
  disabledOn?: Date;
  tags?: ITag[];

  schemaVersion?: number;
  createdOn?: Date;
  updatedOn?: Date;
}

const AccountSchemaFields: Record<keyof IAccount, any> = {
  _id: Types.ObjectId,
  _type: { type: String, default: "Account" },

  pai: String,
  disabledOn: { type: Date, required: false },
  tags: { type: [TagSchema], required: false },

  schemaVersion: { type: Number, default: 1 },
  createdOn: Date,
  updatedOn: Date,
};

I feel like it's never a good rule to keep a type of any laying around, but not sure what to replace it with, if that is indeed the problem.

Or is this error saying that I am missing a Mongoose IAccount Document?

CodePudding user response:

You are actually not using the types properly, for the ClientSession issue, this is because you are not passing dbSession as a {session: dbSession}

so the solution for that is

 await newAccount.save({ session: dbSession});

while the second one, the error is straightforward, newAccount does not fully satisfy IAccount,

but you can circumvent this , by doing the below

async function createAccount(
    dbSession: ClientSession,
    pai: string
  ): Promise<Pick<IAccount, '_id'>> {
    const newAccount = new Model(
      new (class implements IAccount {
        _id = Types.ObjectId();
        _type: "Account" = "Account";
        pai = pai;
      })()
    );
    await newAccount.save({session: dbSession});
    return newAccount
  }
  • Related