0

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

1 回答 1

0

好奇你为什么不想简单地做而不需要魔法 - 简单的问题是什么..

var models = require('./models');
var user = new models.User();
于 2012-08-01T00:01:48.300 回答