1

我的代码在循环之外工作正常:

var localFolder = '/home/transcripts',
    apiHost = 'https://someserver'
    apiCall = '/path',
    sharedSecret = 'secret';
/* includes */

var sys = require('sys'),
    fs = require('fs'),
    exec = require('child_process').exec,
    querystring = require('querystring'),
    crypto = require('crypto')
    request = require('request');
var file = "index_07_03_2013_1209576.csv";

console.log('Pushing CSV to API: ' + file);
var r = request.post(
    apiHost + '/' + apiCall,
    {   form: {
            'filename': file,
            'accessToken': crypto.createHash('md5').update(file + sharedSecret).digest('hex')
        }
    },
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body);
        }
        console.log(error);
        console.log(response);
    }
);

file = "index_07_02_2013_1548773.csv";

console.log('Pushing CSV to API: ' + file);
var r = request.post(
    apiHost + '/' + apiCall,
    {   form: {
            'filename': file,
            'accessToken': crypto.createHash('md5').update(file + sharedSecret).digest('hex')
        }
    },
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body);
        }
        console.log(error);
        console.log(response);
    }
);

file = "index_07_02_2013_1548773.csv";

console.log('Pushing CSV to API: ' + file);
var r = request.post(
    apiHost + '/' + apiCall,
    {   form: {
            'filename': file,
            'accessToken': crypto.createHash('md5').update(file + sharedSecret).digest('hex')
        }
    },
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body);
        }
        console.log(error);
        console.log(response);
    }
);

但是,当我尝试将其移入 forEach 循环时,服务器从未收到任何请求。我确实看到输出表明 CSV 文件正在被推送到 API,但请求似乎从未到达服务器。这是代码:

#!/usr/local/bin/node

/* environment vars */

var localFolder = '/home/transcripts',
    apiHost = 'https://someserver'
    apiCall = '/path',
    sharedSecret = 'secret';

/* includes */

var sys = require('sys'),
    fs = require('fs'),
    exec = require('child_process').exec,
    querystring = require('querystring'),
    crypto = require('crypto')
    request = require('request');

var counter = 0;

fs.readdir(localFolder, function(err, files) {

    files.forEach(function(file) {

        if (file.substr(-3, 3) == 'csv') {


            console.log('Pushing CSV to API: ' + file);
            request.post(
                apiHost + '/' + apiCall,
                {   form: {
                        'filename': file,
                        'accessToken': crypto.createHash('md5').update(file + sharedSecret).digest('hex')
                    }
                },
                function (error, response, body) {
                    if (!error && response.statusCode == 200) {
                        console.log(body);
                    }
                    console.log(error);
                    console.log(response);
                }
            );
            console.log(request);

        }

        counter++;

    });

    if (counter >= files.length) {

        process.exit(0);

    }

});

任何人都有任何想法可能是什么问题?

4

1 回答 1

0

我猜循环版本会中断,因为您process.exit(0)一旦counter >= files.length评估为真就运行。

它实际上破坏了预期功能的原因可能是因为您request非常快速地分离了调用(它们是异步的),并且一旦您完成了该操作,您就退出了程序。我的猜测是,由于您实际上并没有等待请​​求完成(即回调完成时),因此您过早地退出了程序。

您需要的基本上是一个等待组,并且确保在所有回调完成之前不要退出程序。

尝试这样的事情:

// imports, etc
// ...

var counter = 0;

function done() {
  counter--;

  if(counter === 0) {
    process.exit(0);
  }
}

var callback = function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  }
  console.log(error);
  console.log(response);

  done();
}


fs.readdir(localFolder, function(err, files) {
  counter = files.length;

  files.forEach(function(file) {
    if (file.substr(-3, 3) == 'csv') {
      console.log('Pushing CSV to API: ' + file);
      var data = { 
        form: {
          'filename': file,
          'accessToken': crypto.createHash('md5').update(file + sharedSecret).digest('hex')
        }
      }

      request.post(apiHost + '/' + apiCall, , data, callback);
    }
  }
);
于 2013-07-18T18:48:26.093 回答