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;