搜尋

首頁  >  問答  >  主體

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

迷茫迷茫2767 天前367

全部回覆(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
  • 取消回覆