Rumah  >  Soal Jawab  >  teks badan

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

迷茫迷茫2764 hari yang lalu362

membalas semua(1)saya akan balas

  • 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;

    balas
    0
  • Batalbalas