3

I am new to nodejs/mongo/mongoose and i am trying to do a very simple thing. I have the following schemas:

var authorSchema = mongoose.Schema({
    name: String,        
});
Author = mongoose.model('Author', authorSchema);

var bookSchema = mongoose.Schema({
    title: String,        
    isbn: String,
    pages: Number,        
    author: { type : mongoose.Schema.ObjectId, ref : 'Author', index: true }
});
Book = mongoose.model('Book', bookSchema);

I want to create a list of authors with id, name and book count for each author. I have something like this:

exports.author_list = function(req, res){
    Author.find({}, function (err, authors){
        var author_array = Array();
        for (var i=0;i<authors.length;i++){
            var author_obj = new Object();
            author_obj.id = authors[i]._id;
            author_obj.name = authors[i].name;
            author_obj.count = 0; //here is the problem 
            author_array[i] = author_obj;
        }
        res.writeHead(200, { 'Content-Type': 'application/json' });
        res.write(JSON.stringify({ authors: author_array }));
        res.end();

    });
}

I know how to do a query for the count. My problem is how to do a loop of the authors and populate the output with asynchronous callbacks. What is the proper way to implement this in nodejs fashion?

Thanks

2
  • What is Request (seems to be a schema but you haven't shown it)? What is requests (used in the for-loop)? What do you want to count with author_obj.count? What do you mean with "loop of the authors and populate the output with asynchronous callbacks"? Commented Apr 20, 2013 at 13:12
  • Sorry, Request was a leftover from my original code. I switched my code to authors/books model for a cleaner example. Commented Apr 20, 2013 at 13:30

1 Answer 1

5

I think you'd want to use something like async to coordinate those requests; map() seems to be a good choice:

Author.find({}, function (err, authors) {
  async.map(authors, function(author, done) {
    Book.count({author: author._id}, function(err, count) {
      if (err)
        done(err);
      else
      {
        done(null, {
          id    : author._id,
          name  : author.name,
          count : count
        });
      }           
    });
  }, function(err, author_array) {
    if (err)
    {
      // handle error
    }
    else
    { 
      /*
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.write(JSON.stringify({ authors: author_array }));
      res.end();
      */
      // Shorter:
      res.json(author_array);
    }
  });
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.