我有一段代码收集用户名单:here is the gist
当我运行我的代码时,我收到以下错误(我缺乏javascript技能是这里的主要问题)
child_process.js:927 throw 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
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});
});
};
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;
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
答案 0 :(得分:1)
我遇到了@Abdelkrim irl,我们通过用递归函数替换while(kontinue)
循环来解决他的问题。
比照这里解释的模式:http://www.richardrodger.com/2011/04/21/node-js-how-to-write-a-for-loop-with-callbacks/#.UufBCD0o9hE