Saturday 29 April 2017

Callback newly created array as the result of forEach loop [duplicate]

This question already has an answer here:

I want to pass a newly create array as a callback. I'm creating the array by looping through a series of records and adding various values from them. Once I have gone through all the records I try to pass the array as a callback but it is empty.

I know this is caused by asynchronous environment of node and have read that promises are probably the best solution but I haven't been able to understand how I can get them to work in this scenario.

This is my function:

module.exports.listDiagnostics = function (callback) {

    var completedDiagnostics = [];

    User.find({}, function (err, users) {

        users.forEach(function (currentUser, index, array) {

            exports.getLatestSubmittedDiagnostic(currentUser.user_id, function (latestDiagnostic_id, dateSubmitted) {

                if (latestDiagnostic_id) {

                    Diagnostic.findOne({'_id': latestDiagnostic_id,}).exec(function (err, thisDiagnostic) {

                        var compDiag = [];
                        compDiag.push(thisDiagnostic.email);

                        thisDiagnostic.questions.sort(sortArray('question_id'));

                        var answers = [];
                        for (var z = 0; z < 76; z++) {
                            answers.push(thisDiagnostic.questions[z].answer_id - 1);
                        }

                        compDiag.push(answers);

                        completedDiagnostics.push(compDiag);

                    });
                }
            });

            if (index == array.length -1) {
                callback(completedDiagnostics);
            }
        });
    });

    function sortArray(prop) {
        var sortOrder = 1;
        if (prop[0] === "-") {
            sortOrder = -1;
            prop = prop.substr(1);
        }
        return function (a, b) {
            var result = (a[prop] < b[prop]) ? -1 : (a[prop] > b[prop]) ? 1 : 0;
            return result * sortOrder;
        }
    }

}

What are the possible ways to solve this issue?



via Niall Faucher

No comments:

Post a Comment