模式不是构造函数-猫鼬/节点

时间:2018-08-12 22:12:04

标签: node.js mongodb mongoose

这个问题肯定已经回答了很多次,但是似乎没有一种解决方案对我有用。我是Mongoose / Node的初学者,并且正在关注PluralSight教程,在该教程中标题出现错误。它专门指向bookModel.js的第4行。我已经在下面包括了我的两个JavaScript文件以及package.json,以防我的软件包版本出现问题。

据我所知,我的代码与教程相同,几乎与我能找到的每个解决方案都非常相似,但是我仍然收到相同的错误。

我为这个愚蠢的问题道歉,但是我已经为此苦苦挣扎了一段时间,死在水里。

谢谢

models / bookModel.js

width

app.js

var mongoose = require("mongoose"),
Schema = mongoose.Schema();

var bookModel = new Schema({
  title: {type: String},
  author: {type: String},
  genre: {type: String},
  read: {type: Boolean, default:false}
});

module.exports = mongoose.model("Book", bookModel);

package.json

var express = require("express")
mongoose = require("mongoose");

var db = mongoose.connect("mongodb://localhost/bookAPI");

var Book = require("./models/bookModel");

var app = express();
var port = process.env.port || 3000;

var bookRouter = express.Router();
bookRouter.route("/books")
  .get(function(req, res){
      Book.find(function(err, books){
          if(err){
              res.status(500).send(err);
          }
          else{
              res.json(books);
          }
      })


  });

app.use("/api", bookRouter);

2 个答案:

答案 0 :(得分:1)

问题是:

Schema = mongoose.Schema();

这实际上不是构造函数,因为Schemamongoose.Schema()调用的结果,即伪模式对象。

应该是:

Schema = mongoose.Schema;

答案 1 :(得分:0)

尝试以这种方式设置架构。

var mongoose = require("mongoose"),

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

module.exports = mongoose.model("Book", bookSchema);
相关问题