PHP中文网2017-04-17 13:45:08
var mongoose = require('./config/mongoose.js');
var db = mongoose();
var User = mongoose.model('User');//这里应该是 `User= require('./model/user.server.model.js')`
mongoose.model
是注册user,不会挂在mongoose上面,你的app.js
中的mongoose 是mongoose.js
中export出来的db。
mongoose.js 还可以这么组织model
var mongoose = require('mongoose');
var config = require('./config.js');
module.exports = exports = function(){
var db = mongoose.connect(config.uri);
exports.User = require('../models/user.server.model.js');
exports.Other = require('../models/other.server.model.js');
return export;
};
app.js 这么用
var db = require('./config/mongoose.js')();
var User = db.User;
var Other = db.Other;