Search code examples
node.jsmongodbexpresspugmonk

Display data with Jade: MongoDB + Monk + ExpressJS


I'm trying to show some data from my MongoDB database on the client side, but I keep getting the following error: 8| li 9| #{victim.first-name} > 10| Cannot read property 'first' of undefined

I've been following this tutorial, but for some reason, I keep running into that error message... any ideas?

Relevant pieces of my app.js file:

var mongo = require('mongodb');
var monk = require('monk');
var db = monk('ds012345.mlab.com:12345/dbname');

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

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

app.use('/', routes);
app.use('/users', users);

My index.js file:

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

My datatesting.jade file:

extends layout

block content
  h1.
      List of Victims
      ul
          each victim, i in datatesting
              li
                  #{victim.first-name}

Edit: Here's some sample data retrieved from the Mongo shell. There are only 13 documents in the collection.

{
    "_id": {
        "$oid": "56e5f15f0e1fc43fe589f5a9"
    },
    "first-name": "Tanisha",
    "last-name": "Anderson",
    "age": 37,
    "gender": "F",
    "killed": "Y",
    "image": "http://google.com",
    "street-address": "Ansel Road, Cleveland, Ohio",
    "city": "Cleveland",
    "state": "Ohio",
    "latitude": 41.52067,
    "longitude": -81.6222,
    "date": "11/1/2014"
}

Solution

  • So I figured it out! Turns out that I needed to double-check my indents in my .jade file.

    After copying and re-pasting the example from the aforementioned tutorial, I got this:

    extends layout
    
    block content
        h1.
            Victims
        ul
            each user, i in datatesting
                li
                  img(src= "#{user.image}")
    

    As you can see, the h1 tag is not supposed to be aligned with the ul tag. After that, it was able to print out my data correctly.