我不太清楚这个错误是什么意思:
类型'Document‘缺少’IAccount‘类型中的下列属性:_type,pai
47名返回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;
}
我还得到了一个相关错误,上面写着:
重载3中的1,‘(选项?:SaveOptions >未定义):Promise>',给出了以下错误。类型'ClientSession‘与类型'SaveOptions’没有共同的属性。重载3中的2,‘(选项?:SaveOptions \\未定义的,fn?:Callback> _ undefined):void',给出了以下错误。类型'ClientSession‘与类型'SaveOptions’没有共同的属性。重载3中的3,'(fn?:Callback>区未定义):void',给出了以下错误。类型'ClientSession‘的参数不能分配给'Callback>’的参数.
46例等待newAccount.save(dbSession);
但它似乎与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
从来都不是一个好的规则,但我不知道该用什么来代替它,如果这确实是问题所在。
还是说我丢失了一个猫鼬IAccount文档是错误的呢?
发布于 2021-09-23 22:13:35
您实际上没有正确地使用类型,对于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
}
https://stackoverflow.com/questions/69305348
复制相似问题