【问题标题】:stripe: throw errnoException(process._errno, 'spawn');条纹:抛出 errnoException(process._errno, 'spawn');
【发布时间】:2014-02-19 01:51:05
【问题描述】:

我有一段代码收集用户列表:here is the gist

当我运行我的代码时,我收到以下错误(我缺乏 javascript 技能是这里的主要问题)

  1. 我收集条纹帐户
  2. 我将计数设置为 100
  3. 我收集了前 100 个客户帐户
  4. 我循环 3 直到收集所有帐户(如果 count

child_process.js:927 抛出 errnoException(process._errno, 'spawn'); ^ 错误:生成 EAGAIN

at errnoException (child_process.js:980:11)
at ChildProcess.spawn (child_process.js:927:11)
at exports.spawn (child_process.js:715:9)
at Object.exports.execFile (child_process.js:607:15)
at exports.exec (child_process.js:578:18)
at Object.Stripe.getClientUserAgent (/myProjectDir/node_modules/stripe/lib/stripe.js:125:5)
at Object.StripeResource._request (/myProjectDir/node_modules/stripe/lib/StripeResource.js:175:18)
at Object.list (/myProjectDir/node_modules/stripe/lib/StripeMethod.js:45:10)
at async.series.customers (/myProjectDir/app/lib/stripeKPIs.js:26:30)
at /myProjectDir/node_modules/async/lib/async.js:551:21

StripeDashboard.js

var url = require('url');
var nodedump = require('nodedump').init({ expand: true }).dump;
var stripeKPIs = require('../lib/stripeKPIs.js');
var async = require('async');
module.exports = function (app) {


    app.get('/stripe/dashboard', function (req, res) {


        var parts = url.parse(req.url, true);
        var queryString = parts.query;

        var payingCustomers = -1;
        var apiKey = req.session.user.credentials.access_token;

        console.log('/stripe/dashboard: queryString: ' + JSON.stringify(queryString));
        console.log('/stripe/dashboard: apiKey: ' + apiKey);

        async.waterfall([function (callback) {


            callback(null, stripeKPIs.payingCustomers(apiKey, new Date(2013, 1, 1), new Date(2013, 12, 1)));
        }],
            function (err, aSyncResults) {

                payingCustomers = aSyncResults[0];
                console.log('*********************');
                console.log('callback called! ' + JSON.stringify(payingCustomers));

            });


        res.render('stripe/stripeDashboard.html', {payingCustomers: payingCustomers, session: req.session});

    });
};

StripeKPIs.js

var async = require('async');

function StripeException(message) {
    this.message = message;
    this.name = "StripeException";
}


function payingCustomers(apiKey, startDate, endDate, callback) {
    // see https://stripe.com/docs/api/node#list_customers
    // return(how many customers)
    var kontinue = true;
    var result = []; //json containing all customers.data


    var stripe = require("stripe")(apiKey);
    var count = 100;
    var offset = 0;


    while (kontinue) {


        async.series([function (callback) {

            stripe.customers.list({count: count, offset: offset/*, gte: startDate, lte: endDate*/}, function (err, customers) {
                if (err) {
                    kontinue = false;
                    throw new StripeException('stripe.customers.list error: ' + err);
                }
                else {
                    console.log('collectCustomers: offset: ' + offset + ' - customers: ' + JSON.stringify(customers));
                    callback(null, customers);
                }
            });
        }],
            function (err, aSyncResults) {
                console.log('count(' + count + ') - offset(' + offset + ')');
                console.log('inside the callback');
                if (err) {
                    console.errror('async callback error: ' + err);
                    return next(err);
                }
                var customers = aSyncResults[0];

                result = result.concat(customers.data);
                if (customers.data.length < count) {
                    console.log('customers.data.length(' + customers.data.length + ') < count (' + count + ')');
                    kontinue = false;
                }


                offset = result.length;
                console.log('payingCustomers: offset: ' + offset + ' - result: ' + result.length);
                console.log('aSyncResults: offset: ' + offset + ' - result: ' + aSyncResults[0].data.length);
            });
    }
    return(result);
}

module.exports.payingCustomers = payingCustomers;

console.log 输出

Running "jshint:gruntfile" (jshint) task
>> 1 file lint free.

Running "jshint:lib" (jshint) task
>> 16 files lint free.

Running "jshint:test" (jshint) task
>> 1 file lint free.

Running "nodemon:dev" (nodemon) task
[nodemon] v1.0.13
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: app/**/*
[nodemon] starting `node --debug ./server.js dev`
debugger listening on port 5858
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
'development' === app.get('env')
listening to https://mymachine.local:3001
Connected to mongoose
/stripe/dashboard: queryString: {}
/stripe/dashboard: apiKey: sk_test_1234
startDate(Fri Mar 01 2013) - endDate(Thu Jan 01 2015)
*********************
callback called! undefined


collectCustomers: offset: 0 - customers: {"object":"list","count":14,"url":"/v1/customers","data":[{"object":"customer","created":1333799,"id":"cus_2LeD54sf3k","livemode":false,"description":"My test env","email":"your@email.com","delinquent":false,"metadata":{},"subscription":{"id":"sub_01","plan":{"interval":"year","name":"Standard plan billed yearly","created":13074184,"amount":2000,"currency":"eur","id":"StdYear","object":"plan","livemode":false,"interval_count":1,"trial_period_days":null,"metadata":{}}....]}

count(100) - offset(0)

inside the callback

customers.data.length(14) < count (100)

payingCustomers: offset: 14 - result: 14

aSyncResults: offset: 14 - result: 14

【问题讨论】:

  • console.log 是否产生预期结果?这可能是不正确的:offset = result.length;,因为它会导致您始终获得相同的客户“页面”。尝试使用offset += result.length; 递增另一个问题是循环while (kontinue)。相反,使用递归函数,这可能会解决您的问题。告诉我进展如何
  • 我的另一个问题是为什么你使用async.waterfall 只运行一个函数及其回调,但我想你在这里向我们展示了简化的代码,你在生产代码中运行了更多的函数。
  • @mef 这不是一段简化的代码;我尝试了许多解决方案,并且 async.waterfall 是我的代码的最新版本。如有必要,可能会被删除

标签: javascript node.js callback stripe-payments


【解决方案1】:

我遇到了@Abdelkrim irl,我们通过用递归函数替换 while(kontinue) 循环解决了他的问题。

参见。此处解释的模式:http://www.richardrodger.com/2011/04/21/node-js-how-to-write-a-for-loop-with-callbacks/#.UufBCD0o9hE

【讨论】:

    猜你喜欢
    • 2016-03-05
    • 2014-11-13
    • 2017-01-24
    • 2016-09-01
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 2016-04-06
    相关资源
    最近更新 更多