Passing external data into a template

  • 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:

20151102-14:06:39.530(0)? { object: ‘list’,
I20151102-14:06:39.531(0)? data:
I20151102-14:06:39.532(0)? [ { id: ‘in_172fv02Sv5VqXz0f0gdu24nR’,
I20151102-14:06:39.532(0)? object: ‘invoice’,
I20151102-14:06:39.532(0)? amount_due: 1000,
I20151102-14:06:39.532(0)? application_fee: null,
[…]

  • 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! :smile:

If I wasn’t the one seeking help I might mention that’s your own fault for deviating from the actual language meteor uses :stuck_out_tongue: (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);
  });
});

undefined

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!

Ha, I noticed that actually, but same thing for just logging result!

If the Meteor.method is returning a value, and you are assigning the result to a reactive var, the data will be there!

I did wonder if this was the case… but whatever I try doesn’t work. Is there an a way to dump variables in blaze so I can check?

I’m currently testing with:

{{#with invoices}}
  {{ data.[0].amount_due }}
{{/with}}

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

You can dump it during the helper autorun:

Template.BillingSettings.helpers({
  invoices: function () {
    var data = Template.instance().getInvoices.get();
    console.log(data);
    return data;
  }
});

Ah, already tried that, a big fat

undefined

:sweat_smile: there’s something wrong somewhere, probably can’t help you much more without a repo!

Damnit - that’s not news I wanted to hear! :stuck_out_tongue:

If it helps you can have the 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;
    }

    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.

Just a lucky guess:

self.Template.instance().getInvoices.set(result);

I mean, self = this would just make it same as this.getInvoices.set(result) instead of Template.instance().getInvoices.set(result).

That’s how I understand it.

But if console.log(result) returns undefined then problem surely lies in a different place.

this already refers to the template instance inside onCreated

1 Like

Yeah, you’re not returning anything :stuck_out_tongue:

Follow the answer here: http://stackoverflow.com/questions/26226583/meteor-proper-use-of-meteor-wrapasync-on-server

Yes I was wondering right now, why aren’t you returning anything. :stuck_out_tongue:

Ah balls!

           

See? If you were using CS, it would be working. CS returns everything by default. :stuck_out_tongue:

I hope it’s working fine for you now!

Haha, I guess that was it! :stuck_out_tongue:

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 :frowning:

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;
  }
1 Like

Glad you got it working! The sync stuff in Meteor got me too the first time round :stuck_out_tongue: