我目前在我的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