Reputation: 1351
Let me rephrase my question. How do I get the data which comes back in an array to render. (I am using handlebars)
The Code:
//http verbs
module.exports = {
get: function(req, res) {
gm(req.url);
app.set('view engine', 'hbs');
//session check
if (session checks out<-not actual code){
//get mongoose data here
var bmdata = bmquery.execFind(function(err, docs){
console.log(docs);
var model = {
layout:'blog.hbs',
BlogModel: docs,
};
//render page
res.render('blog', model);
});
}
else {
console.log('illegal user');
console.log('redirection in progress');
res.redirect('/login');
}
}
};
The console.log of docs comes back in an array like so: [{document 1},{document 2}]
Could you also do this dynamically so that I do not have to put the array position.
My handlebars looks like this:
{{BlogModel[0].title}}
{{BlogModel[0].content}}
{{BlogModel[1].title}}
{{BlogModel[1].content}}
The Problem Data comes back in an array and I cant get it to render out dynamically or at all.
Upvotes: 1
Views: 366
Reputation: 1368
var BlogModel = mongoose.model('blogmodel', BlogPost, 'blogmodel');
var bms = BlogModel.find({ "date" : { $gte : new Date("2011-01-01T00:00:00Z")}} ).limit(1);
module.exports = {
get: function(req, res) {
//ExecFind is asnychornous, so you need to wait to get the data to render it.
bms.execFind(function(err, docs) {
console.log(docs);
var model = {
layout:'blog.hbs',
BlogModel: docs
};
res.render('blog', model);
});
}
};
Also, this should be on your configuration of the app, not stranded on a module in the app
app.set('view engine', 'hbs');
And as someone say, you should learn how to handle the asyn nature of node and understand a little bit more the concept of callbacks.
Upvotes: 1