DEV Community

wasim15185
wasim15185

Posted on • Edited on

please explain some conceptual part for express js and mongoose

##code is here
if i 'const homeModel = require('../models/home')'; define indside 'router.post' then homeModelis gives me whole home model but when
'const homeModel = require('../models/home');' define in global then homeModel gives a Empty Objects / {} why it is happening like that

             const router = require('express').Router();
          const userModel = require('../models/user');
         const commentModel = require('../models/comment');

     //importing middleware
     const { AuthMiddleWare } = require('../middleware/middleware');

 router.post('/comment/:id', AuthMiddleWare, (req, res) => {
// important
const homeModel = require('../models/home'); **<--talking about this** 

const postId = req.params.id;
// console.log(postId);
const { comment } = req.body;
console.log(homeModel);
console.log(userModel);
console.log(commentModel);

homeModel.findById(postId, (err, post) => {
    if (err) {
        console.log(err);
    } else {
        //finding Current  user
        userModel.findOne({ email: req.session.passport.user }, (err, user) => {
            if (err) {
                console.log(err);
            } else {
                //founded Current user

                // post

                // create}
                              })
                       })

Top comments (0)