节点异步映射实用程序无法按预期处理大量数据

时间:2016-05-24 11:28:12

标签: node.js asynchronous

我要做的是从数据库中获取fitbit用户然后逐个处理并为每个用户调用三个api端点。一切正常,但当用户数量大幅增加时,我无法为某些用户持续刷新令牌。

   var fitbit_instances = []; // this array will have instances for each user
    User.find(where, {}, function (err, fitbit_users) {
        async.map(fitbit_users, function (v, cbk) {
            // make instance of api for each user
            fitbit_instances.push(new Fitbit(config.fitbit));
            var index = fitbit_instances.length - 1;
            // then save data for each endpoint
            async.series([
                    function (callback) {
                        // api end point 1
                        fitbit_instances[index].request({
                            uri: "https://api.fitbit.com/1/user/-/activities/date/" + $newdate + ".json",
                            method: 'GET'
                        }, function (err, data, res) {
                            if (err) {
                                callback(err, null);
                            } else {
                                callback(null, udata);
                            }
                        });
                    },
                    function (callback) {
                        // api end point 2
                        fitbit_instances[index].request({
                            uri: "https://api.fitbit.com/1/user/-/activities/date/" + $newdate + ".json",
                            method: 'GET'
                        }, function (err, data, res) {
                            if (err) {
                                callback(err, null);
                            } else {
                                callback(null, udata);
                            }
                        });
                    },
                    function (callback) {
                        // api end point 3
                        fitbit_instances[index].request({
                            uri: "https://api.fitbit.com/1/user/-/activities/date/" + $newdate + ".json",
                            method: 'GET'
                        }, function (err, data, res) {
                            if (err) {
                                callback(err, null);
                            } else {
                                callback(null, udata);
                            }
                        });
                    },
                ],
                function (err, data) {
                    cbk(null);
                });
        }, function (err) {
            if (err) {
                res.status(500).json({message: 'Database error occurs.', error: err});
            }
            else {
                res.status(200).json({message: 'Success', data: [], error: {}});
            }
        });
    });

0 个答案:

没有答案