繁体   English   中英

猫鼬查找结果和异步

[英]Mongoose Find Results and Async

我正在编写一个NodeJS脚本,该脚本将通过Heroku的调度程序每小时运行一次。 我正在查询我拥有的Mongo实例(mongohq / compose),然后对这些结果进行处理。 我正在使用Mongoose.js和find()命令。 这将返回结果数组。 对于这些结果,我需要执行其他查询以及一些其他异步处理(发送电子邮件等)。

长话短说,由于节点的异步特性,我需要等到所有处理完成后再调用process.exit() 如果我不这样做,则脚本会提前停止,并且不会处理整个结果集。

问题是我在这一点上有一个圣诞树效果(5个嵌套的asnyc调用)。

通常,我会使用async.js库解决此问题,但通过这么多的回调方法却无法解决这个问题。

如何在退出脚本之前确保整个过程完成?

这是我正在使用的代码(注意:下面还将lodash用作_ ):

Topic.find({ nextNotificationDate: {$lte: moment().utc()}}, function (err, topics) {
    if (err) {
        console.error(err);
        finish();
    } else {

        _.forEach(topics, function (topic, callback) {

            User.findById(topic.user, function (err, user) {
                if (err) {
                    // TODO: impl logging
                    console.error(err);
                } else {

                    // Create a new moment object (not moment.js, an actual moment mongoose obj)
                    var m = new Moment({ name: moment().format("MMM Do YY"), topic: topic});
                    m.save(function(err) {
                        if(err) {
                            // TODO: impl logging
                            console.error(err);
                        } else {

                            // Send an email via postmark
                            sendReminderTo(topic, user, m._id);

                            // Update the topic with next notification times.
                            // .. update some topic fields/etc
                            topic.save(function (err) {
                                if(err) {
                                    console.error(err);
                                } else {
                                    console.log("Topic updated.");
                                }
                            })
                        }
                    })
                }
            });
            console.log("User: " + topic.user);
        });
    }
});

使代码混乱的部分原因是else语句的使用。 如果返回错误,则不需要else语句并为每个回调保存4行缩进。 它本身将使事情更具可读性。

使用异步:

Topic.find({nextNotificationDate: {$lte: moment().utc()}}, function (err, topics) {
    if (err) {
        console.error(err);
        return finish(err);
    }

    async.each(topics, function(topic, topicCallback) {

        async.auto({

            user: function (callback) {

                User.findById(topic.user, callback);
            },

            moment: function(callback) {

                var m = new Moment({name: moment().format("MMM Do YY"), topic: topic});
                m.save(callback);
            },

            topic: ["moment", "user", function (callback, results) {

                var m = results.moment;
                var user = results.user;

                sendReminderTo(topic, user, m._id);
                topic.save(callback);
            }]

        }, function(err) {
            if (err) {
                return topicCallback(err);
            }

            console.log("Topic updated.")
            return topicCallback();
        });
    }, function(err) {
        if (err) {
            console.error(err);
            return finish(err);
        }

        return finish();
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM