我尝试返回mongoose文档,但随后我得到这个错误:
类型'(个人文档& { _id:ObjectId; })[]'遗漏型别' IPersonDocument '的下列属性:名字、姓氏、组、$assertPopulated和其他52个.ts(2740)
IPersonDocument是IPerson
和Mongoose.Document
的扩展
这是我代码:
repo.ts
export async function findByName(name: String): Promise<IPersonDocument>{
return await PersonsModel.find({ firstName: name }) //problem here
}
type.ts
import { Document, Model } from "mongoose";
export interface IPerson {
firstName: string;
lastName: string;
age?: number;
email?: string,
groups: [String],
dateOfEntry?: Date;
}
export interface IPersonDocument extends IPerson, Document { }
export interface IPersonModel extends Model<IPersonDocument> { }
moduel.ts
import { model } from "mongoose";
import { IPersonDocument } from "../../../type/person.types";
import PersonsSchema from "./person.schema";
export const PersonsModel = model<IPersonDocument>("persons", PersonsSchema)
1条答案
按热度按时间pcww981p1#
添加倾斜()
返回等待人员模型。查找({名字:名称}).lean();
它将DOC对象转换为js对象。