猫鼬模型TypeError:模式不是构造函数

时间:2018-10-08 08:34:59

标签: node.js mongodb mongoose

我看到了这个问题: mongoose TypeError: Schema is not a constructor

但是我仍然无法将模型与Mongoose结合使用。

当我尝试这样做时:

const mongoose = require('mongoose').connect(`mongodb://${process.env.DB_USER}:${process.env.DB_PASSWORD}@localhost:27017/${process.env.DB_NAME}`, {useNewUrlParser: true});
  const Schema = mongoose.Schema;

  var blogSchema = new Schema({
    title:  String,
    author: String,
    body:   String,
    comments: [{ body: String, date: Date }],
    date: { type: Date, default: Date.now },
    hidden: Boolean,
    meta: {
      votes: Number,
      favs:  Number
    }
  });

  var Blog = mongoose.model('Blog', blogSchema);

我收到以下错误:

TypeError: Schema is not a constructor
    at module.exports (/Users/razbuchnik/Projects/taxi4you/server/resources/permissions/api/v1-update.js:30:20)
    at Layer.handle [as handle_request] (/Users/razbuchnik/Projects/taxi4you/server/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/razbuchnik/Projects/taxi4you/server/node_modules/express/lib/router/route.js:137:13)
    at /Users/razbuchnik/Projects/taxi4you/server/app/middlewares/permission.js:27:7
    at /Users/razbuchnik/Projects/taxi4you/server/node_modules/mongojs/lib/collection.js:50:5
    at runInAsyncScope (/Users/razbuchnik/Projects/taxi4you/server/node_modules/mongojs/lib/cursor.js:198:5)
    at /Users/razbuchnik/Projects/taxi4you/server/node_modules/mongojs/lib/cursor.js:205:5
    at handleCallback (/Users/razbuchnik/Projects/taxi4you/server/node_modules/mongojs/node_modules/mongodb/lib/utils.js:120:56)
    at /Users/razbuchnik/Projects/taxi4you/server/node_modules/mongojs/node_modules/mongodb/lib/cursor.js:683:5
    at handleCallback (/Users/razbuchnik/Projects/taxi4you/server/node_modules/mongojs/node_modules/mongodb-core/lib/cursor.js:171:5)
    at nextFunction (/Users/razbuchnik/Projects/taxi4you/server/node_modules/mongojs/node_modules/mongodb-core/lib/cursor.js:691:5)
    at /Users/razbuchnik/Projects/taxi4you/server/node_modules/mongojs/node_modules/mongodb-core/lib/cursor.js:602:7
    at queryCallback (/Users/razbuchnik/Projects/taxi4you/server/node_modules/mongojs/node_modules/mongodb-core/lib/cursor.js:232:18)
    at /Users/razbuchnik/Projects/taxi4you/server/node_modules/mongojs/node_modules/mongodb-core/lib/connection/pool.js:469:18
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)

注意:这是来自Mongoose官方网站和GitHub存储库的副本。

3 个答案:

答案 0 :(得分:3)

问题在于mongoose不是猫鼬对象,而是connect promise

应该是:

const mongoose = require('mongoose')
const Schema = mongoose.Schema;

mongoose.connect(`mongodb://${process.env.DB_USER}:${process.env.DB_PASSWORD}@localhost:27017/${process.env.DB_NAME}`, {useNewUrlParser: true});

请注意,mongoose.connect可能不适用于模型,但不适用于父模块,因为可能有多个模型模块。

答案 1 :(得分:1)

错误是因为您的const mongoose具有mongoose.connect而不是mongoose的实例。

尝试一下:

const mongoose = require('mongoose');
const connect = mongoose.connect(`mongodb://${process.env.DB_USER}:${process.env.DB_PASSWORD}@localhost:27017/${process.env.DB_NAME}`, {useNewUrlParser: true});
  const Schema = mongoose.Schema;
  var blogSchema = new Schema({
    title:  String,
    author: String,
    body:   String,
    comments: [{ body: String, date: Date }],
    date: { type: Date, default: Date.now },
    hidden: Boolean,
    meta: {
      votes: Number,
      favs:  Number
    }
  });

  var Blog = mongoose.model('Blog', blogSchema);

希望,这对您有帮助。

答案 2 :(得分:0)

如果其他任何人仍然有问题,但仍然无法解决,也许在您的模型文件中,而不是在module.exports=中,也许您有module.export=

这可能对操作没有帮助,但是这使我痛苦了30分钟,因此将其粘贴到这里。