我不是很清楚这个错误在说什么:
Type 'Document<any,>'缺少以下属性从类型'IAccount': _type, pai
返回newAccount;
错误是关于这个方法的:
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;
}
我也得到一个相关的错误,说:
重载1 of 3, '(options?): SaveOptions | undefined):Promise
<any,>>',给出了以下错误。类型"ClientSession"与类型"SaveOptions"没有共同的属性。超载2/3(选项?): SaveOptions |未定义fn吗?: Callback <any,>>| undefined): void',给出了下面的错误。类型"ClientSession"与类型"SaveOptions"没有共同的属性。重载3的3,'(fn?: Callback 比;| undefined): void',给出以下错误。类型为"ClientSession"的参数不能赋值给类型为"Callback <任何,任何>>"的参数。
但它似乎与AccountSchema
模型有关,any
在其尖括号中:
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,
};
我觉得这从来都不是一个好的规则,保持一种类型的any
铺设,但不确定用什么来代替它,如果这确实是问题。
或者这个错误说我缺少一个Mongoose IAccount文档?
您实际上没有正确使用类型,对于ClientSession
问题,这是因为您没有将dbSession
传递为{session: dbSession}
所以它的解是
await newAccount.save({ session: dbSession});
第二个,错误很直接,newAccount
不完全满足IAccount
,
,但是你可以通过执行下面的
来规避这个问题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
}