Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Express/Dust.js - View array of data

Express/Dust.js - View array of data


I currently have my homepage showing the 6 oldest Stories posts from MongoDB

I use the below for the query from Mongo:

var query = Story

Then I'm using this to pass the information to dust.js

app.get('/', function(req, res){
    query.exec(function(err, story) {
        if (err) console.log(err);
        res.render('index', {
        title: 'Test!',
        storytitle1: story[0].title,
        storylines1: story[0].lines,
        storyid1: story[0].sid,
        storyslug1: slugs(story[0].title),   
        storytitle2: story[1].title,
        storylines2: story[1].lines,
        storyid2: story[1].sid,
        storyslug2: slugs(story[1].title), 
        storytitle3: story[2].title,
        storylines3: story[2].lines,
        storyid3: story[2].sid,
        storyslug3: slugs(story[2].title),    
        storytitle4: story[3].title,
        storylines4: story[3].lines,
        storyid4: story[3].sid,   
        storyslug4: slugs(story[3].title), 
        storytitle5: story[4].title,
        storylines5: story[4].lines,
        storyid5: story[4].sid,  
        storyslug5: slugs(story[4].title),  
        storytitle6: story[5].title,
        storylines6: story[5].lines,
        storyid6: story[5].sid,  
        storyslug6: slugs(story[5].title)     

Then on i have the below on the .dust template





Basically for each of the 6 i render them individually, it works perfectly fine, but i'm wondering if there is a better/cleaner way to do this? Because i want to implement another page '/all/ which shows for example the last 20 stories and i don't want to write them all individually within the template.

Problem courtesy of: Tam2


Try this.


app.get('/', function(req, res){
    query.exec(function(err, story) {
        if (err) console.log(err);
        for (var i = 0; i 




{/lines} {/stories}
Solution courtesy of: Vadim Baryshev


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Express/Dust.js - View array of data


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription