1

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.

2
  • You would be better studying JavaScript more throughly. Seems you don't know scope and async nature of JavaScript. Also you should read documentation of expressjs.com throughly. Commented Jul 27, 2012 at 11:51
  • Yea Sorry I phrased that poorly, I fixed it. Yes I know what a scope is and was previously asking if I could pass the generated variable from in the scope to out of it. (which I cant) :) Commented Jul 27, 2012 at 20:31

1 Answer 1

1
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.

Sign up to request clarification or add additional context in comments.

2 Comments

I updated my code, It works if inside of my model variable I have docs[0] or 1 but it will not load more than one array value at once and I want it to load around 5.
.limit(1); <- That. Change it to 5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.