无法显示Mongo数据库中的数据

时间:2016-06-06 04:11:34

标签: javascript node.js mongodb server pug

我遵循本教程:http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/

当我在我的mongo终端进行http://localhost:3000/userlist时,我的记录会打印出来 - 所以我知道我有要显示的记录。

当我在浏览器中extends layout block content h1. User List ul each user, i in userlist li a(href="mailto:#{user.email}")= user.username 时,我会收到" User List"在顶部 - 但我没有得到任何用户数据。

它应该看起来像this

问题:我的userlist.jade文件中的链接是否未指向数据库数据?

userlist.jade

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

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

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

module.exports = router;

index.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

// new code
// we want to talk to mongodb, use monk to do it, databasebase is lcoated at localhost:27017/nodetest1
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/nodetest1');

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

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

// Make our db accessible to our router - new code 
app.use(function(req,res,next){
    req.db = db;
    next();
});


// telling Express what route files to use 
app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

// our master app exporting its app object. All modules export an object whch can easily be called elsewhere in code. 
module.exports = app;

app.js

 private static double TotalPayRoll;  -> private double TotalPayRoll;
 private static double Salary;        -> private double Salary;

1 个答案:

答案 0 :(得分:0)

我遇到了同样的错误,然后我解决了这个问题:

  

app.js

var db = monk('localhost:27017 / nodetest1');

  

将localhost更改为127.0.0.1

并确保集合名称为nodetest1