猫鼬模型TypeError:模式不是构造函数 [英] Mongoose model TypeError: Schema is not a constructor
本文介绍了猫鼬模型TypeError:模式不是构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看到了这个问题: mongoose TypeError:架构不是构造函数
I have seen this question: mongoose TypeError: Schema is not a constructor
但是我仍然无法将模型与Mongoose结合使用.
However I still cannot using Models with 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存储库的复制副本.
Note: this is a copy pase from official Mongoose site and GitHub repo.
推荐答案
The problem is that mongoose
is not Mongoose object but 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
可能不适用于模型,但不适用于父模块,因为可能有多个模型模块.
Notice that mongoose.connect
may be suitable not for model but parent module because there could be multiple model modules.
这篇关于猫鼬模型TypeError:模式不是构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文