0

I have code in app.js

    var Song = db.model('Song');
    var Album = db.model('Album');

I want to render to index.jade with 2 variables are list of song and list of album
I use query like this

Song.find({}, function( err, docs ){
// .........
}
Album.find({}, function( err, docs ){
// .........
}

So, what should i do to store list of song and list of album to variables and render to index.jade with 2 lists

1 Answer 1

1

I think you mean something like this:

function( req, res ) { // whatever your "controller" function is
  Song.find({}, function( err, songs ){
    Album.find({}, function( err, albums ){
      res.render('index', { song_list: songs, album_list: albums });
    });
  }); 
}

Then just iterate and markup your song_list and album_list arrays in the template.

Note that this is synchronous and therefore slower than an async approach, but it should do what you want. To go the async route, consider using a library like this to defer res.render until both queries are done: https://github.com/kriszyp/promised-io

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

1 Comment

Hi, Jed, could u help me to resolve problem in HERE

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.