0

I have been using this post as a guide in order to create some NodeJS code to connect to a MongoDB instance, authenticate, then calculate the difference between two timestamps, and then send this to a nunjucks template. My code is:

app.get('/test', function(req,res){

    db.authenticate('USER', 'PASSWORD', function(err, result) {
    assert.equal(true, result);

    });

    db.test('TEST').find({Total_items:{$exists:true}}).forEach(function(thing) {
    var date1 = new Date(thing.DTG_posted_UTC);
    var date2 = new Date(thing.read_dtg);
    var dateDiff = date1.getTime() - date2.getTime();


    var data = ({_id:thing._id,DTG_posted_UTC:thing.DTG_posted_UTC, read_dtg:thing.read_dtg ,dateDiff:dateDiff});

    });



    return res.render('test', data);

    });

});

The Express server loads fine, but when I visit the 'test' page the app crashes, and returns: "ReferenceError: data1 is not defined".

I know the query is working, as I can log.console the results, and that works as expected (see below).

app.get('/test', function(req,res){

    db.authenticate('USER', 'PASSWORD', function(err, result) {
    assert.equal(true, result);

    });

    db.test('TEST').find({Total_items:{$exists:true}}).forEach(function(thing) {
    var date1 = new Date(thing.DTG_posted_UTC);
    var date2 = new Date(thing.read_dtg);
    var dateDiff = date1.getTime() - date2.getTime();


    var data = ({_id:thing._id,DTG_posted_UTC:thing.DTG_posted_UTC,read_dtg:thing.read_dtg ,dateDiff:dateDiff});
    console.log(data);
    });



    return res.render('test');

    });

Unfortuantly I cannot figure out how to pass the results of the Mongo query to my nunjucks template. Any help would be much appreciated.

Community
  • 1
  • 1
ColSan
  • 11
  • 4

1 Answers1

0
app.get('/test', function(req, res, next){
    db.authenticate('USER', 'PASSWORD', function (err, result) {
        assert.equal(true, result);
        // if (err)
        //     return next(err);

        db.test('TEST').find({Total_items:{$exists:true}}).forEach(function(thing) {
            var date1 = new Date(thing.DTG_posted_UTC);
            var date2 = new Date(thing.read_dtg);
            var dateDiff = date1.getTime() - date2.getTime();           

            var data = {
                _id: thing._id,
                DTG_posted_UTC: thing.DTG_posted_UTC,
                read_dtg: thing.read_dtg,
                dateDiff: dateDiff
            };

            res.render('test', data);
        });
    });
});
Aikon Mogwai
  • 4,954
  • 2
  • 18
  • 31
  • Thanks @Aikon Mogwai. I've tried your fix. The server launches fine, but when visiting the page /test it crashes and throws a "Can't set headers after they are sent" error. – ColSan Dec 05 '16 at 10:54
  • I also added a console.log(data) before the "res.render('test', data), and it's displaying the expected data. The issue seems to be with the res.render. – ColSan Dec 05 '16 at 10:55
  • If console.log prints data then problem inside template `test`. In template you must use `{{dateDiff}}` (not `{{data.dateDiff}}`) to print difference. – Aikon Mogwai Dec 05 '16 at 12:38
  • Doesn't make a difference. The template test current has this in it:

    Test

    – ColSan Dec 05 '16 at 14:07
  • And what do you expect if template don't contains vars? – Aikon Mogwai Dec 05 '16 at 16:41
  • I would have expected the template to still render, but just without the data (I didn't think not including data sent to templates would stop them rendering.) Having played with the template now, and included {{dateDiff}} the template renders the first item, and then the app crashes with the "Can't set headers after they are sent" error. – ColSan Dec 05 '16 at 17:46
  • Often `Can't set headers after they are sent` means that you call render twice. Check callback calls. – Aikon Mogwai Dec 06 '16 at 23:23