0

I am trying to run two different mongoose queries and display the result in same ejs page. I searched a lot and finally found the async method. How I tried this is below.

js file

router.get('/CreateSurvey', function(req, res, next) {

async.parallel([
        function(callback) {
            QBank.find( function ( err, classes, count ){
                classes : classes  
            });
        },
       function(callback) {
            QBank.findOne({_id:"H001"},  function ( err, questions, count ){     
                questions : questions
            });
        }
    ], function(err) { 
        if (err) return next(err);
        res.render('CreateSurvey', QBank);
    });

});

But when I refresh '/CreateSurvey' page it does not render. I have already downloaded async module and required in my js file. Where am I wrong?

Kabilesh
  • 1,000
  • 6
  • 22
  • 47
  • I don't find anything wrong in ASYNC.PARALLEL what i think the issue is in QBank why are you passing it in res.render() ? – Shumi Gupta Jan 26 '17 at 06:45
  • I was following work by some others. I think both 'classes' and 'questions' will be assigned in same name 'QBank'. See the final answer in http://stackoverflow.com/questions/26402781/nodejs-mongoose-render-two-models-from-collections – Kabilesh Jan 26 '17 at 06:58

2 Answers2

1

Firstly, What is that classes : classes and questions : questions lines in your code? What are you trying to do?

The callback parameter of each task function (the functions in array) should be called inside the task to indicate completion of each task with success or error.

The optional main callback (the third function in your code), where you are doing the actual rendering, will be called only after all the tasks have completed.

In your code callback is not called inside the task. As a result the final callback is also never getting called. In fact, in your code only the first *task gets executed and nothing happens beyond that.

Read more about async.parallel in documentation

Santanu Biswas
  • 4,699
  • 2
  • 22
  • 21
  • There is a question bank in my survey system. When the user goes to 'Import questions', I want to display the question type (class) and when the user clicks the type, the questions inside that type must be displayed. So, to display this by javascript hide & show in the same ejs page I want to send find all and find one to same ejs page. – Kabilesh Jan 26 '17 at 08:27
  • So what is that syntax you are using for `.find()` in `mongoose`? You are reading all the questions in your database in one call? You want to do that? – Santanu Biswas Jan 26 '17 at 08:43
  • You need to also get your code syntax right. Please see @MukeshSharma's reply. I suggest you first read relevant documentation of `mongoose` and `async`. – Santanu Biswas Jan 26 '17 at 08:46
1

There are some issues with the code, you aren't calling callback corresponding to async call. Try the following:

router.get('/CreateSurvey', function(req, res, next) {
  async.parallel({
    classes: function(callback) {
      QBank.find(function(err, classes, count){
        callback(classes);
      });
    },
    questions: function(callback) {
      QBank.findOne({_id:"H001"},  function (err, questions, count ){     
        callback(questions);
      });
    }
  }, function(err, result) { 
    if (err) return next(err);
    res.render('CreateSurvey', result); // { classes : [c1, c2] , questions : [q1, q2] }
  });
});
Mukesh Sharma
  • 8,914
  • 8
  • 38
  • 55