我目前在我的Mongoose/NodeJS应用程序的/models/models.js文件中有所有模型(模式定义)。Mongoose&NodeJS项目的文件结构
我想分离这些成不同的文件,例如: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” *
的思考?
我写猫鼬胶模块,可以帮助组织你的猫鼬型号:https://开头github.com/xpepermint/mongoose-glue – xpepermint 2014-08-30 17:10:40