Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript mongoose schema.method不工作:类型脚本错误TS2339:Property';myMethod';不存在于类型';模型<;MyModelI>';_Typescript_Mongoose - Fatal编程技术网

Typescript mongoose schema.method不工作:类型脚本错误TS2339:Property';myMethod';不存在于类型';模型<;MyModelI>';

Typescript mongoose schema.method不工作:类型脚本错误TS2339:Property';myMethod';不存在于类型';模型<;MyModelI>';,typescript,mongoose,Typescript,Mongoose,我想将mongoose与TypeScript一起使用。另外,我想扩展模型功能以添加新方法 但当调用tsc传输文件时,我得到: spec/db/models/match/matchModelSpec.ts(47,36): error TS2339: Property 'findLeagueMatches' does not exist on type 'Model<MatchModelI>'. spec/db/models/match/matchModelSpec.ts(47,36

我想将
mongoose
TypeScript
一起使用。另外,我想扩展模型功能以添加新方法

但当调用tsc传输文件时,我得到:

spec/db/models/match/matchModelSpec.ts(47,36): error TS2339: Property 
'findLeagueMatches' does not exist on type 'Model<MatchModelI>'.
spec/db/models/match/matchModelSpec.ts(47,36):错误TS2339:属性
类型“Model”上不存在“findLeagueMatches”。
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);
从“/MatchInterface”导入{MatchInterface};
从“mongoose”导入{Schema,model,model,Document};
导出接口MatchModelI扩展了MatchInterface,文档{
findLeagueMatches(locationName:String,leagueName:String):承诺;
}
导出let matchSchema:Schema=新架构({
开始时间:{type:Date,必需:true},
位置:{type:Schema.Types.ObjectId,ref:'location'},
联盟:{type:Schema.Types.ObjectId,ref:'league'}
});
matchSchema.methods.findLeagueMatches=函数(locationName:String,leagueName:String){
//...
返回比赛;
};
导出常量匹配:模型=模型(“匹配”,匹配模式);
它可以工作:

import {Schema, Model, model, Document} from "mongoose";
import {LeagueInterface} from "./leagueInterface";
import {MatchModelI, Match} from "../match/matchModel";
import {LocationModelI} from "../location/locationModel";

export interface ILeagueDocument extends Document {
    name: string;
    location: LocationModelI;
}

export interface LeagueModelI extends Model<ILeagueDocument>{
    getAllMatches(): Promise<MatchModelI[]>
}

export let leagueSchema: Schema = new Schema({
    name: { type: String, required: true },
    location: {type: Schema.Types.ObjectId , ref: 'Location', required: true},
});

const getAllMatches = async function ():Promise<MatchModelI[]>{
    return Match.find({league: this._id}).exec();
};

leagueSchema.method('getAllMatches', getAllMatches);


/** @class Match */
export const League: LeagueModelI = model<ILeagueDocument, LeagueModelI>("League", leagueSchema);
从“mongoose”导入{Schema,Model,Model,Document};
从“/LeagueInterface”导入{LeagueInterface}”;
从“./Match/matchModel”导入{MatchModelI,Match};
从“./location/locationModel”导入{LocationModelI};
导出接口ILeagueDocument扩展文档{
名称:字符串;
地点:LocationModelI ;;
}
导出接口LeagueModelI扩展模型{
getAllMatches():承诺
}
导出let leagueSchema:Schema=新架构({
名称:{type:String,必需:true},
位置:{type:Schema.Types.ObjectId,ref:'location',必需:true},
});
const getAllMatches=async函数():Promise{
return Match.find({league:this.\u id}).exec();
};
leagueSchema.method('getAllMatches',getAllMatches');
/**@级比赛*/
导出常量联盟:LeagueModelI=模型(“联盟”,LeagueChema);