admin管理员组

文章数量:1317898

I know some JS but I'm new to Node.js. I know this is a very mon 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');

// unment 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?

I know some JS but I'm new to Node.js. I know this is a very mon 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');

// unment 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?

Share Improve this question edited Jul 7, 2017 at 8:12 jason asked Jul 7, 2017 at 7:19 jasonjason 7,17438 gold badges125 silver badges209 bronze badges 4
  • gte property of router in index.js is the problem ? – yashpandey8055 Commented Jul 7, 2017 at 7:22
  • Add var router = express.Router(); in index.js as well. – Fahad Nisar Commented Jul 7, 2017 at 7:23
  • @yashpandey it didn't work. – jason Commented Jul 7, 2017 at 7:33
  • @yashpandey I'm already having that line in index.js – jason Commented Jul 7, 2017 at 7:39
Add a ment  | 

2 Answers 2

Reset to default 3

Changing code to this worked:

app.use(function(req,res,next){
    req.db = db;
    next();
});
app.use('/', index);
app.use('/userlist', users);

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');

本文标签: javascriptCannot read property 39get39 of undefined in NodejsStack Overflow