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?

2
  • 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() ? Commented Jan 26, 2017 at 6: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 stackoverflow.com/questions/26402781/… Commented Jan 26, 2017 at 6:58

3 Answers 3

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

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

3 Comments

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.
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?
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.
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] }
  });
});

Comments

0

if you prefer using Promises (which is more modern and might be simpler), you can use Promise.all:

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

try {
    const classesPromise = QBank.find().exec();
    const questionsPromise = QBank.findOne({_id: "H001"}).exec();

    const [classes, questions] = await Promise.all([classesPromise, questionsPromise]);

    res.render('CreateSurvey', { classes: classes, questions: questions });
} catch (err) {
    next(err);
}})

Comments

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.