I wanted to extract data from mongodb and pass it to the view. Everything seems to work, but instead of seeing all 10,000 entries, I see only one. I feel very close to solving it, but I'm stuck ... I use node-Mongodb-native, express and dot.js to achieve my goal.
Here is my app.js, look for app.get () where all the action is:
var mongodb = require('mongodb'),
serverdb = new mongodb.Server('127.0.0.1', 27017, {}),
dbName = new mongodb.Db('test', serverdb, {});
var express = require('express'),
doT = require('doT'),
app = express.createServer(),
pub = __dirname + '/public',
view = __dirname + '/views';
app.configure(function(){
app.set('views', view);
app.set('view options', {layout: false});
app.set('view engine', 'html');
app.use(app.router);
});
app.register('.html',doT, {
compile: function(str, opts){
return function(locals){
return str;
}
}
});
app.get('/', function(req, res){
dbName.open(function (error, client) {
var collection = new mongodb.Collection(client, 'personnel');
collection.find().each(function(err, data){
if (err){return res.end('error!'+err);}
if (data){
res.render('index.html',{data:data._id});
} else {
res.end();
}
});
});
});
app.configure('production', function(){
app.use(express.errorHandler());
});
app.listen(3000);
console.log('Express server listening on port %d in %s mode', app.address().port, app.settings.env);
And here is my html:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Index</title>
</head>
<body>
{{=it.data}}
</body>
</html>
Please help if you can. Thank.