Meteor Braintree - create a client token through the Meteor method

I am trying to get Braintree Payments to work in the Meteor app. I was stuck in an attempt to return the result of creating a marker (server side, through the Meteor method), which will be used on the client side.

I tried this:

/server/braintree.js

Meteor.methods({
  createClientToken: function() {

    var token = gateway.clientToken.generate({
        customerId: this.userId
      }, function(err, response) {
          clientToken = response.clientToken
          return clientToken
        }
      )

    console.log(token)
    return token
  }
})

which returns true.

I also tried this:

Meteor.methods({
  createClientToken: function() {

    var clientToken
    gateway.clientToken.generate({
        customerId: this.userId
      }, function(err, response) {
          clientToken = response.clientToken
        }
      )

    console.log(clientToken)
    return clientToken
  }
})

What returns undefined.

function(err, response)called asynchronously, yes? If so, this will be the explanation of the problem. It seems like trying to return a value from an asynchronous function is a bit of a sore point in Javascript. I read some SO answers to it (like this , this, and this ), but no one seems to have led me in the right direction.

, , , , Meteor wrapAsync, ? ( SO- ), " , .

.

Update:

Braintree Meteor . ( @Nick Tomlin )

+4
1

: Braintree:)

Meteor, , @mrak, clientToken.generate , .

clientToken undefined, console.log(clientToken) , clientToken clientToken.generate. , , , , ( ).

, Meteor.wrapAsync , , untested.

Meteor.methods({
  createClientToken: function() {
    var createToken = Meteor.wrapAsync(gateway.clientToken.generate, gateway.clientToken);

    var response = createToken({});

    return response.clientToken;
  }
});

Braintree + Meteor, ( , , GH, !)

+5

All Articles