Schema不是构造函数在节点js中定义模型时

时间:2017-09-20 07:28:02

标签: javascript node.js

我正在为书店创建应用程序

var  _mongoose  =  require("mongoose");
var  Schema =  _mongoose.Schema();
var  books  =  new Schema ({
 title : { type : String},
author : { type : String},
genre  : { type: String},
read  : { type : Boolean, default  : false}
});

module.exports = _mongoose.Model('Books',books);

此代码出错:

var  books  =  new Schema ({
           ^

TypeError: Schema is not a constructor

由于我是节点js的新手,请建议如何继续。

版本在mongoose和express

{
"express": "^4.15.4",

"mongoose": "^4.11.12"
}

谢谢

1 个答案:

答案 0 :(得分:4)

一个工作示例:

var _mongoose  = require("mongoose");
var Books  = new _mongoose.Schema({
    title: { type: String },
    author: { type: String },
    genre: { type: String },
    read: { type: Boolean, default: false }
});

module.exports = _mongoose.model('Books', Books);

你正在调用Schema两次,而mongoose.model全是小写的。查看文档http://mongoosejs.com/docs/