0
server.get('/', function(req, res) {
    Counter.find({}, function(err, result) {
        if (!(err)) {
            res.render('index', {'lol' : result});
        }
    });
});

I'm trying to get my app to display the contents of the whole database and this is what I came up with. Counter is a mongoose model. The database contains some items inserted prior to the execution of the program and one item inserted in the app itself.

Something is really iffy conceptually to me (I'm new to node), I think render() is being executed before find() which is why I'm not getting a result, but I can't think of a solution. Any help or push in the right direction is greatly appreciated. :)

2
  • 1
    res.render will definitely execute after the find completes as you've correctly put it in the find callback. What's specifically not working? Commented Feb 6, 2014 at 3:59
  • The problem was with the db, not the code. Thanks for the help! :) Commented Feb 6, 2014 at 14:58

1 Answer 1

2

What is your view code?

Your implementation is correct, you should try to do debug.

server.get('/', function(req, res) {
  Counter.find({}, function(err, result) {
    if (!(err)) {
        console.log('Debug: ' + JSON.stringify(result) );
        res.render('index', {lol : result});
    }
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

The problem was with my db, I was trying to access a collection which contained no data. In hindsight, silly mistake. Thanks for the help. :)

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.