0

I'm trying to create an array of query results so that I can use that array to populate one of my handlebars pages. Each index in the array holds Comments with a unique attribute. I currently have it set up like this:

This kicks the whole thing off:

    //num is determined above
    Comment.getComments(num, function(err, comments){
        if(err){
            console.log("ERROR");
        }
        console.log(comments);
        res.render('page-i-want-to-populate-with-comments', {com : comments});
    });

The getComments function calls this (in my models/comment.js):

module.exports.getComments = function(num, callback){
    var comArr = [];
    for(var i = 0; i < num; i++){
        var uniqueId = i;
        Comment.find({commentId : uniqueId}, function(err, items){
            comArr.push(items);
        });
    }
    callback(comArr);
}

Ideally I would then go to page-i-want-to-populate-with-comments.handlebars and:

{{#each com}}
<div class="comment-wrapper>
    {{#each this}}
    <div class="comment">
        <p>{{this.text}}</p>    //the text contained in the comment
    </div>
    {{/each}}
</div>
{{/each}}

My points of failure now are that comArr is empty when sent through callback() which causes an error in the callback function.

I get that this is probably confusing but this is the best I can explain it. I've been stuck on this for a while so any advice would be appreciated.

jbaumann
  • 181
  • 6

0 Answers0