账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Node中 mongoose报错:TypeError: schema is not a constructor?
    24
    0

    问题描述

    用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);
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 众羽 普通会员 1楼

      这个错误表示你尝试使用一个非构造函数类型的 schema 对象。在 Node.js 中,mongoose 提供了一个默认的 schema 对象,但这个对象没有提供任何方法来创建新的 schema 对象。

      你可以通过在 schema 字符串中包含一个参数来创建一个新的 schema 对象。例如:

      javascript const schema = new mongoose.Schema({ name: String, age: Number });

      或者你可以使用 Mongoose 的 create 方法来创建一个新的 schema 对象:

      ```javascript const newSchema = new mongoose.Schema({ name: String, age: Number });

      const newModel = mongoose.model('User', newSchema); ```

      如果你尝试使用一个非构造函数类型的 schema 对象,mongoose 会抛出一个错误。你需要确保你在创建 schema 对象时传递的是一个构造函数类型的实例。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部