TS 2322:typescript和mongoose.model作为函数的返回类型
新的打字脚本。我试图创建一个mongoose模型工厂,它基于一个角色返回mongoose模型。然后,我将在数据访问层(DAL)中使用此模型继续我的业务层。问题是我得到了以下错误。TS2322:类型“typeof(../../model)不可分配给类型”模型。类型“typeof”(“../../model”)中缺少属性“findById” 我是否错过了一个明确的演员阵容 型号代码TS 2322:typescript和mongoose.model作为函数的返回类型,typescript,mongoose,Typescript,Mongoose,新的打字脚本。我试图创建一个mongoose模型工厂,它基于一个角色返回mongoose模型。然后,我将在数据访问层(DAL)中使用此模型继续我的业务层。问题是我得到了以下错误。TS2322:类型“typeof(../../model)不可分配给类型”模型。类型“typeof”(“../../model”)中缺少属性“findById” 我是否错过了一个明确的演员阵容 型号代码 import * as mongoose from "mongoose"; let StudentSchema =
import * as mongoose from "mongoose";
let StudentSchema = new mongoose.Schema({
id:Number,
username: String,
email: String,
sectors: [{
sectorName:String,
QP: String
}]
}, {
timestamps: {
createdAt: 'created_at'
}
});
export default mongoose.model('StudentNotification', StudentSchema);
**我的模型工厂代码**我在返回学生通知代码中得到错误
import * as mongoose from "mongoose";
import * as StudentNotification from './models/student';
export class ModelFactory {
private userRole:string;
constructor(role:string){
this.userRole = role;
}
Create():mongoose.Model<mongoose.Document>{
switch(this.userRole){
case "Student":{
return StudentNotification;
break;
}
default :{
return null;
}
}
}
}
import*作为“猫鼬”中的猫鼬;
从“./models/student”导入*作为StudentNotification;
导出类模型工厂{
私有用户角色:字符串;
构造函数(角色:字符串){
this.userRole=role;
}
Create():mongoose.Model{
开关(this.userRole){
案例“学生”:{
返回学生通知;
打破
}
默认值:{
返回null;
}
}
}
}
要导入默认导出,需要使用以下语法:
import * as mongoose from "mongoose";
import StudentNotification from './models/student'; // HERE !
function Create():mongoose.Model<mongoose.Document>{
switch(this.userRole){
case "Student":{
return StudentNotification;
}
default :{
return null;
}
}
}
您必须将导入语句更改为:
import StudentNotification from './models/student';
因为当您导出它时,您会使用默认值
关键字导出它
export default mongoose.model('StudentNotification', StudentSchema);
export default mongoose.model('StudentNotification', StudentSchema);