假设我有一个模块models.js
:
exports.User = mongoose.model('User', UserSchema);
exports.Question = mongoose.model('Question', QuestionSchema);
exports.Answer = mongoose.model('Answer', AnswerSchema);
exports.Comment = mongoose.model('Comment', CommentSchema);
现在我想在另一个文件中使用它:
var models = require('./models');
var User = models.User;
var Question = models.Question;
var Answer = models.Answer;
var Comment = models.Comment
// then use them
var user = new User();
我必须声明我定义的所有模型,这很无聊models.js
。
有什么方法可以简化它,我不需要再次声明模型:
var models = require('./models');
// !!! do some magic
var user = new User();