I have a loop that gets an array of users and then tries to get the information of each one. With this code below, if I have two users, it returns the same information (the last user) twice. Therefore, when I try to update and save the user information, it only saves the last user's information.
for (i = 0; i < users.length; i++) {
var amount = users[i].monthlyAmount;
// var stripeID = users[i].stripeID;
// if the amount charged is greater than 0, charge the customer the correct amount
if(amount != '0') {
var stripeID = users[i].stripeID;
accountID = users[i];
// stripe function to charge customer
stripe.charges.create({
amount: amount,
currency: "usd",
customer: stripeID,
description: "Monthly charge"
}, function(err, charge) {
if (err) return (err);
// if there are no errors with charging the customer, proceed to reset the monthly amount
if(!err) {
console.log(accountID);
accountID.monthlyAmount = '0';
accountID.save();
}
});
}
}
Here's what the output of this is:
{ billingInfo: 'true',
createdAt: Sun Nov 16 2014 14:05:21 GMT-0600 (CST),
email: 'a@a.com',
encryptedPassword: '*removed',
monthlyAmount: 1000,
propertyCount: 0,
stripeID: '*removed',
updatedAt: Sun Nov 16 2014 15:10:59 GMT-0600 (CST),
id: '54690381c03a265b07c99564' }
{ billingInfo: 'true',
createdAt: Sun Nov 16 2014 14:05:21 GMT-0600 (CST),
email: 'a@a.com',
encryptedPassword: '*removed',
monthlyAmount: '0',
propertyCount: 0,
stripeID: '*removed',
updatedAt: Sun Nov 16 2014 15:10:59 GMT-0600 (CST),
id: '54690381c03a265b07c99564' }