首页  >  问答  >  正文

node.js - node expressjs中怎么配置和组织mongoose模块?

迷茫迷茫2764 天前360

全部回复(1)我来回复

  • PHP中文网

    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;

    回复
    0
  • 取消回复