假设我有一个模块 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();