Node中 mongoose报错:TypeError: schema is not a constructor?

问题描述

用mongoose创建了一个学生Schame,但是运行的时候报错,错误在第三行的Schema;

TypeError: schema is not a constructor

看了代码没找到错,请大家帮忙看看,谢谢!

相关代码

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;
const courseInf = new Schema({
    teacher:String,
    courseName:String
});
const student = new Schema({
    name:String,                //姓名
    class:String,                //班级
    stunum:String,                //学号
    oldpassword:String,           //旧密码
    newpassword:String,           //新密码
    phonenum:String,              //手机号
    course:[courseInf]           //课程
});

module.exports.stu = mongoose.model('student',student);
阅读 6.8k
2 个回答

这个文件应该没有问题,感觉你定位的文件位置不对,最好将错误信息贴出来。
请全局搜索一下,是不是有的地方这样写的 Schema = new mongoose.Schema;,即多写了一个new

试试把Schema = mongoose.Schema;换成 const { Schema } = mongoose;

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题