node.js中的foreach异步功能

部长

我想遍历每个学生,然后执行两个查询,这两个查询的执行应同步,首先是第一个,然后是第二个查询,因为这取决于第一个。

我已经写了一些代码,但似乎根本不起作用:

Student.find({ status: 'student' })
    .populate('student')
    .exec(function (err, students) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        }

        _.forEach(students, function (student) {
            async.waterfall(
                [
                    function (callback) {
                        console.log('first ' + student.firstName);
                        Student.find({ "_id": student.id }, callback);
                    },
                    function (student, callback) {
                        console.log('second '+ student[0].firstName);
                        WorksnapsTimeEntry.find({
                            "student": {
                                "$in": student.map(function (el) {
                                    return el._id
                                })
                            }
                        }, callback);
                    }
                ],
                function (err, results) {
                    if (err) {
                        // do something
                    } else {
                        // results are the matching entries
                        console.log('third');
                        var totalMinutes = 0;
                        var totalAvgLevelActivity = 0;
                        var counter = 0;
                        _.forEach(results, function (item) {
                            _.forEach(item.timeEntries, function (item) {
                                if (item.duration_in_minutes) {
                                    totalMinutes = totalMinutes + parseFloat(item.duration_in_minutes[0]);
                                }

                                if (item.activity_level) {
                                    totalAvgLevelActivity = totalAvgLevelActivity + parseFloat(item.activity_level[0]);
                                    counter++;
                                }
                            });
                        });

                        var obj = {};
                        obj.studentId = 'test';
                        obj.firstName = 'test';
                        obj.lastName = 'test';
                        obj.municipality = 'test';
                        obj.totalMinutes = totalMinutes;
                        obj.totalAvgLevelActivity = totalAvgLevelActivity / counter;
                        arrayReports.push(obj);
                        // console.log('not yet finished.');
                    }
                }
            );
        });

        res.json(arrayReports);
        console.log('finished.');

任何人都知道如何在Node.js中实现此目标

沙努尔

mongoose承诺,您无需使用async简单的forEach就可以处理流程或lodash了。并且通过_id请求查找是无用的,您已经有一个Student对象:

Student.find({ status: 'student' })
    // .populate('student') // why this?
    .then(function (students) {
        // build an array of promises
        var promises = students.map(function (student) {
            return WorksnapsTimeEntry.find({
                "student": student._id;
            });
        });

        // return a promise to continue the chain
        return Promise.all(promises);
    }).then(function(results) {
        // results are the matching entries
        console.log('third');
        var totalMinutes = 0;
        var totalAvgLevelActivity = 0;
        var counter = 0;
        _.forEach(results, function (item) {
            _.forEach(item.timeEntries, function (item) {
                if (item.duration_in_minutes) {
                    totalMinutes = totalMinutes + parseFloat(item.duration_in_minutes[0]);
                }

                if (item.activity_level) {
                    totalAvgLevelActivity = totalAvgLevelActivity + parseFloat(item.activity_level[0]);
                    counter++;
                }
            });
        });

        var obj = {};
        obj.studentId = 'test';
        obj.firstName = 'test';
        obj.lastName = 'test';
        obj.municipality = 'test';
        obj.totalMinutes = totalMinutes;
        obj.totalAvgLevelActivity = totalAvgLevelActivity / counter;
        arrayReports.push(obj);
        // console.log('not yet finished.');
        res.json(arrayReports);
        console.log('finished.');
    }).catch(function(err) {
        return res.status(400).send({
            message: errorHandler.getErrorMessage(err)
        });
    });

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在node.js中管理嵌套的异步foreach循环

来自分类Dev

在Node.js中异步foreach和猫鼬

来自分类Dev

如何使Node JS的句柄异步功能在Node JS中同步

来自分类Dev

Node.js-寻找异步功能

来自分类Dev

异步和等待功能Node.js

来自分类Dev

Node.js / Express异步功能

来自分类Dev

如何在Node.js中通过异步调用重用测试功能?

来自分类Dev

如何设置时间限制以在node.js中运行异步功能?

来自分类Dev

setInterval node.js中的异步和同步功能有什么区别

来自分类Dev

需要在Slack App Node.js中的异步功能内获取REST API

来自分类Dev

在Node.js中,是否可以异步运行中间件功能?

来自分类Dev

如何在Node.js中执行一系列同步和异步功能

来自分类Dev

功能-Node.js中的异步映射,递归和回调的组合

来自分类Dev

使用forEach时,与node.js异步流混淆。

来自分类Dev

使用async.forEach和异步操作的Node.js

来自分类Dev

在node.js中异步循环

来自分类Dev

Node.js中的异步函数

来自分类Dev

Node.js中的异步混乱

来自分类Dev

在Node.js中运行异步过程

来自分类Dev

Node.js中的异步混乱

来自分类Dev

Node.JS-使用异步功能处理传入的请求

来自分类Dev

Node.js:水线+ Caolan /异步:绑定功能

来自分类Dev

如何在node.js中使此功能异步

来自分类Dev

Node.JS-使用异步功能处理传入的请求

来自分类Dev

Node.js中的调用导出功能

来自分类Dev

功能中的Node.js请求

来自分类Dev

Node.js中的调用导出功能

来自分类Dev

包装node.js同步功能是否使其性能与异步等效功能相同?

来自分类Dev

在Node.js中的事件处理程序中异步等待