MIssingSchemaError:Schema尚未注册模型“Emp”

时间:2016-08-06 15:11:51

标签: node.js mongodb mongoose

我尝试了不同的堆栈溢出解决方案,但无法解决此问题。

这是我的代码

var mongoose = require('mongoose');

var schema = new mongoose.Schema({
    name:{
        type:String,
        required:true
    },
    email:{
        type:String,
        required:true,
        lowercase:true
    }
});
mongoose.connect('mongodb://localhost:27017/test');
//parameters are model name,schema,collection name
var Emp = mongoose.model('Emp','schema','users');

1 个答案:

答案 0 :(得分:2)

您在模型方法的第二个参数中使用了一个字符串,它需要Schema

这解决了您的问题:

var Emp = mongoose.model('Emp',schema,'users');

这些是您必须使用的参数类型:

name String model name

架构架构

collection String name(可选,从模型名称引出)

skipInit Boolean是否跳过初始化(默认为false)

此处提供更多信息 http://mongoosejs.com/docs/api.html#index_Mongoose-model