【发布时间】:2012-07-25 17:55:36
【问题描述】:
在 nodejs 中 require 一只猫鼬 Schema 的最佳方式是什么?
最初我在 app.js 文件中有这些,但随着模型的增多,它变得有点大而且笨拙。
现在我想将它们移动到models 文件夹并使用Model = require('./models/model') 将它们导入到 app.js 中
如何让Model 填充实际模型?
(exports = mongoose.model(...) 失败并给了我一个空白对象;exports.model = mongoose.model(...) 要求我执行 Model.model 才能访问它——这些都不是所需的行为)
===
编辑1
所以基本上我已经采取了
var mongoose = require('mongoose');
var Schema = mongoose.Schema, ObjectId = Schema.ObjectId;
var UserSchema = new Schema({
username: String,
password: String,
first_name: String,
last_name: String,
email: String
});
User = mongoose.model('User', UserSchema);
并将其放入./models/user.js
我如何获得它,使其相当于在 app.js 中拥有它?
【问题讨论】:
标签: node.js model mongoose require