0
我想使用mongoose
和TypeScript
。另外,我想扩展模型功能以添加新方法。mongoose schema.method不起作用:TypeScript错误TS2339:属性'myMethod'在类型'上不存在'型号<MyModelI>'
但是,当调用TSC到transpile文件获取:
spec/db/models/match/matchModelSpec.ts(47,36): error TS2339: Property
'findLeagueMatches' does not exist on type 'Model<MatchModelI>'.
MatchModel.ts:
import {MatchInterface} from "./matchInterface";
import {Schema, model, Model, Document} from "mongoose";
export interface MatchModelI extends MatchInterface, Document {
findLeagueMatches(locationName: String, leagueName: String): Promise<MatchModelI[]>;
}
export let matchSchema: Schema = new Schema({
startTime: {type: Date, required: true},
location: {type: Schema.Types.ObjectId, ref: 'Location'},
league: {type: Schema.Types.ObjectId, ref: 'League'}
});
matchSchema.methods.findLeagueMatches = function(locationName: String, leagueName: String){
//...
return matches;
};
export const Match: Model<MatchModelI> = model<MatchModelI>("Match", matchSchema);