目前,我的Mongoose/NodeJS应用程序的所有模型(模式定义)都在/ models /models.js文件中。
我想把它们拆分成不同的文件,比如: user_account.js,profile.js等等。然而,我似乎不能这样做,因为我的控制器崩溃了,一旦我拆分了这些类,就会报告“找不到模块”。
我的项目结构如下:
/MyProject
/controllers
user.js
foo.js
bar.js
// ... etc, etc
/models
models.js
server.js
我的models.js文件的内容如下所示:
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
mongoose.connect('mongodb://localhost/mydb');
var UserAccount = new Schema({
user_name : { type: String, required: true, lowercase: true, trim: true, index: { unique: true } },
password : { type: String, required: true },
date_created : { type: Date, required: true, default: Date.now }
});
var Product = new Schema({
upc : { type: String, required: true, index: { unique: true } },
description : { type: String, trim: true },
size_weight : { type: String, trim: true }
});
我的user.js文件(控制器)如下所示:
var mongoose = require('mongoose'),
UserAccount = mongoose.model('user_account', UserAccount);
exports.create = function(req, res, next) {
var username = req.body.username;
var password = req.body.password;
// Do epic sh...what?! :)
}
如何将模式定义分解为多个文件,并从控制器中引用它?当我引用它时(在模式在一个新文件中之后),我得到这个错误:
*错误:尚未为模型"user_account".*注册架构
有什么想法?
发布于 2012-02-11 05:57:28
这是一个示例app/models/item.js
var mongoose = require("mongoose");
var ItemSchema = new mongoose.Schema({
name: {
type: String,
index: true
},
equipped: Boolean,
owner_id: {
type: mongoose.Schema.Types.ObjectId,
index: true
},
room_id: {
type: mongoose.Schema.Types.ObjectId,
index: true
}
});
var Item = mongoose.model('Item', ItemSchema);
module.exports = {
Item: Item
}
要从app/controllers/items.js
中的项控制器加载它,我会这样做
var Item = require("../models/item").Item;
//Now you can do Item.find, Item.update, etc
换句话说,在您的模型模块中定义模式和模型,然后只导出模型。使用相对要求路径将模型模块加载到控制器模块中。
要建立连接,请在服务器启动代码(server.js
或其他任何代码)的早期进行处理。通常,您需要从配置文件或环境变量中读取连接参数,如果未提供配置,则缺省为开发模式localhost。
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost');
发布于 2013-01-18 00:15:37
这里的几个答案真的帮助我开发了一种替代方法。最初的问题是关于只拆分Schema定义,但我更喜欢将Schema和Model定义捆绑在同一个文件中。
这主要是Peter的想法,只是通过覆盖module.exports来导出模型定义,以减少从控制器访问模型的繁琐程度:
项目布局:
MyProject
/controllers
user.js
foo.js
bar.js
// ... etc, etc
/models
Item.js
server.js
models/Item.js将如下所示:
var mongoose = require("mongoose");
var ItemSchema = new mongoose.Schema({
name: {
type: String,
index: true
}
});
module.exports = mongoose.model('Item', ItemSchema);
// Now `require('Item.js')` will return a mongoose Model,
// without needing to do require('Item.js').Item
你可以在一个控制器中访问模型,比如user.js,比如:
var Item = require(__dirname+'/../models/Item')
...
var item = new Item({name:'Foobar'});
别忘了调用mongoose.connect(..)在server.js中,或者其他您认为合适的地方!
发布于 2015-09-21 04:19:24
我最近回答了一个关于这个问题的Quora问题。http://qr.ae/RoCld1
我发现非常好的并且节省了需要调用的的数量是将你的模型组织到一个单独的目录中。确保每个文件只有一个模型。
在与模型相同的目录中创建一个index.js文件。将此代码添加到其中。确保添加必要的fs要求
var fs = require('fs');
/*
* initializes all models and sources them as .model-name
*/
fs.readdirSync(__dirname).forEach(function(file) {
if (file !== 'index.js') {
var moduleName = file.split('.')[0];
exports[moduleName] = require('./' + moduleName);
}
});
现在,您可以调用所有模型,如下所示:
var models = require('./path/to/models');
var User = models.user;
var OtherModel = models['other-model'];
https://stackoverflow.com/questions/9230932
复制相似问题