0

I know some JS but I'm new to Node.js. I know this is a very common error but I couldn't find the source of the error since I lack debugging skills in Node.js. Here is my app.js :

var index = require('./routes/index');
var users = require('./routes/userlist');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/userlist', users);

Here is my userlist.js :

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = router;

Here is my index.js:

var express = require('express');
var router = express.Router();

router.get('/userlist', function(req, res) {
    var db = req.db;
    var collection = db.get('usercollection');
    console.log(collection);
    collection.find({},{},function(e,docs){
        res.render('userlist', {
            "userlist" : docs
        });
    });
});

Here is userlist view :

extends layout

block content
    h1.
        User List
    ul
        each user, i in userlist
            li
                a(href="mailto:#{user.email}")= user.username

I appreciate if you tell me where the problem is or tell me how I can detect the problem. Thanks in advance.

EDIT:

In console I get this :

GET /userlist 500 296.280 ms - 1501

EDIT 2: It gives me the error at this line :

var collection = db.get('usercollection');

EDIT 3: In index.js, I see that

router.get('/userlist', function(req, res) {
    var db = req.db;

req.db is undefined. Why is it undefined? Do you have any idea?

4
  • gte property of router in index.js is the problem ? Commented Jul 7, 2017 at 7:22
  • Add var router = express.Router(); in index.js as well. Commented Jul 7, 2017 at 7:23
  • @yashpandey it didn't work. Commented Jul 7, 2017 at 7:33
  • @yashpandey I'm already having that line in index.js Commented Jul 7, 2017 at 7:39

2 Answers 2

3

Changing code to this worked:

app.use(function(req,res,next){
    req.db = db;
    next();
});
app.use('/', index);
app.use('/userlist', users);
Sign up to request clarification or add additional context in comments.

Comments

3

Change your code accordingly:

userlist.js

var express = require('express').Router();
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = router;

index.js

var express = require('express').Router();
var router = express.Router();

router.get('/userlist', function(req, res) {
    var db = req.db;
    var collection = db.get('usercollection');
    console.log(collection);
    collection.find({},{},function(e,docs){
        res.render('userlist', {
            "userlist" : docs
        });
    });
});

You should use var router = require('express').Router(); instead of var router = require('express');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.