Show all data coming from mongodb and display it in doT.js templating engine

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:

    /**
 * Mongo DB
 */
var mongodb = require('mongodb'),
    serverdb = new mongodb.Server('127.0.0.1', 27017, {}),
    dbName = new mongodb.Db('test', serverdb, {});
/**
 * Module dependencies.
 */
var express = require('express'),
    doT = require('doT'),
    app = express.createServer(),
    pub = __dirname + '/public',
    view =  __dirname + '/views';

// Configuration
app.configure(function(){
    app.set('views', view);
    app.set('view options', {layout: false});
    app.set('view engine', 'html');
    app.use(app.router);
});

//Simple templating
app.register('.html',doT, {
    compile: function(str, opts){
        return function(locals){
            return str;
        }
    }
});


//----------------- This is where the problem is i think ---------------
app.get('/', function(req, res){
    dbName.open(function (error, client) {
        var collection = new mongodb.Collection(client, 'personnel');
        collection.find().each(function(err, data){
            //Error check
             if (err){return res.end('error!'+err);}
            //Data
             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.

0
source share
3 answers

, , , each() toArray(), . . , , . , .

My app.js:

EDIT: .

    //Replace this code with the one in my question to get full file
    //----------------- This is where the problem was ---------------
    app.get('/', function (req, res) {
        dbName.open(function (error, client) {
            var collection = new mongodb.Collection(client, 'some_collection');
            collection.find().limit(300).toArray(function (err, dataObjArr) {
                var data = '';
                var dataArr = [];
                var i = dataObjArr.length;
                //check for error
                if(err){return res.end('error!'+err);}
                //Data
                if (dataObjArr) {
                    while(i--){
                        dataArr[i] = dataObjArr[i]._id;
                    }
                    data = dataArr.join(' ');
                    res.render('index.html', { returnedData : data });
                }else{
                    res.end();
                }
            });
        });
    });
    //--------------------------------------------------------------

my index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Index</title>
</head>
<body>
    {{=it.returnedData}}
</body>
</html>

, , , .

+1

dot.js. , res.render('index.html',{data:data._id}); , , .

+2

. res.render .

+1
source

All Articles