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?
- 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
2 Answers
Reset to default 3Changing 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
版权声明:本文标题:javascript - Cannot read property 'get' of undefined in Node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742036509a2417324.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论