Yes, not to worry I’m not leading us up a dark alley, the method is definitely fetching the data correctly from Stripe, and I can log it on the server no problem. It’s test data so I can even share it:
The console log right before getInvoices.set(result) is also undefined. I tried using Template.instance() in place of _this but I received an error:
Exception in delivering result of invoking ‘GetStripeInvoices’: TypeError: Cannot read property ‘getInvoices’ of null
So I’m guessing that isn’t the way to go there.
“Somebody more fluent with JS would be handy here, as I’m a CS guy. Please, help! ”
If I wasn’t the one seeking help I might mention that’s your own fault for deviating from the actual language meteor uses (Shakes fist at CoffeeScript)
Ah that looks a bit like an SO I came across, definitely a bit simpler! Unfortunately running a log before setting the result still gives me undefined, so it’s functioning the same:
Template.BillingSettings.onCreated(function () {
this.getInvoices = new ReactiveVar();
var self = this;
Meteor.call('GetStripeInvoices', function (error, result) {
if (error) {
console.log(error);
throw new Meteor.Error(500, "Couldn't get invoices");
}
console.log(self.getInvoices.set(result));
self.getInvoices.set(result);
});
});
result will always return undefined immediately, but as you are assigning result to a reactive variable, you should be getting the returned value once the method has returned the data. Also you are logging the result of the setter method there and not the actual result.
If the Meteor.method is returning a value, and you are assigning the result to a reactive var, the data will be there!
Which given the json structure I think should be working… (even have the data[0] changed to data.[0] as per the spacebars docs). But I’ve tried a few variations on that with no luck (but also while testing methods to get the data into the template in the first place… so
GetStripeInvoices: function () {
var Stripe = StripeAPI(Meteor.settings.stripe.testSecretKey);
if(this.userId) {
var user = Meteor.users.findOne(this.userId);
var customer = user.sub.stripeId;
}
Stripe.invoices.list(
{
customer: customer,
limit: 10
},
Meteor.bindEnvironment(function(error, invoices) {
if (error) {
console.log(error);
throw new Meteor.Error( 500, "Couldn't retreive invoices" );
}
console.log(invoices); // Logs as expected
return invoices
}
));
}
This is where you point out something stupidly obvious that I’ve done/not done.
Edit: Hang on a minute… is where I’m returning the result going to cause an issue in the method? As it’s within another function and the crazy Meteor.bindEnvironment - just had a horrible thought that means it won’t be returning properly.
And yes, literally just got it working using the SO @hellstad linked. Sorry for wasting your time guys, really do appreciate the help. Async and me aren’t friends, and it causes issues sometimes
For completeness here’s the working method:
GetStripeInvoices: function () {
var Stripe = StripeAPI(Meteor.settings.stripe.testSecretKey);
if(this.userId) {
var user = Meteor.users.findOne(this.userId);
var customer = user.sub.stripeId;
}
var GetCustomerStripeInvoices = Meteor.wrapAsync(Stripe.invoices.list,Stripe.invoices);
var result = GetCustomerStripeInvoices({
customer: customer,
limit: 10
})
return result;
}