简体   繁体   中英

How does controllers find models Schema file in mongoose?

I have below structure and I think this is not correct

models/db.js

var mongoose  = require( 'mongoose' );
var mongoURI = 'mongodb://localhost/loc8r';
var mongoDB = mongoose.createConnection(mongoURI);
require('./user');

models/user.js

var mongoose  = require( 'mongoose' );
var Schema =  mongoose.Schema;
var userSchema = new Schema({
    username : { type: String; required: true},
    createdOn: {type: Date, "default": Date.now}
});
mongoose.model('User', userSchema);

controllers/user.js

var mongoose  = require( 'mongoose' );
var user = mongoose.model('User');

My questions are ( co related )

  • How does controllers/users.js pages get in touch with models/users.js

  • What if there is more than one file in models folder, say model/college.js then Do we add more require() lines in db.js?

  • somewhere I see that module.exports are written at the end of the model schema files, does that is right way or above one is correct?

  • Why do we keep adding require mongoose continuously in each page? coming from PHP background , I think that once we add require it will be added in child pages when we include the file. is it not so?

Found the more elegant way which re utilize mongoose object across other model files. I hope this will help newbie which are also facing issue to understand the concept.

use module.exports in main db file which is added in require() in app.js

models/db.js

let mongoose  = require('mongoose');
let mongoURI = 'mongodb://localhost/loc8r';    
module.exports = mongoose.connect(mongoURI,options);    
let conn = mongoose.connection;

Note: I have removed require(..other model file ..) from bottom of db.js


models/user.js

let mongoose  = require( './db' );
let Schema =  mongoose.Schema;
let userSchema = new Schema({
    username: {type: String; required: true},
    createdOn: {type: Date, "default": Date.now}
});
module.exports = mongoose.model('User', userSchema);

Note: add require('./db') in place of mongoose in each other model file and export the schema using module.exports in bottom


controllers/user.js

let User = require('../models/user');

module.exports.userSignup = function(req,res,next) {
    if (req.method == 'POST') {
        var user = new User({
            username: req.body.username,
            password: req.body.password,
            email: req.body.email
        });
        user.save( (err, user) => {
            if(err) return res.status(500).send(err);
            return res.status(200).json(user);
        });
    } 
};

Note: add model files with help of require(...) , so that it will get schema detail and use that variable to save/update new data

feel free to correct if I am wrong or doing the lengthy way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM