javascript - Meteor Braintree -- Create Client Token via Meteor Method -


i'm trying braintree payments working in meteor app. i'm stuck @ trying return result of generating token (server side, via meteor method) used client side.

i've 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've 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   } }) 

which returns undefined.

the function(err, response) being called asynchronously, yes? if so, explanation of problem. seems trying return value asynchronous function bit of pain point in javascript. i've read number of answers on (like this one, this one , this one) none have seemed lead me in right direction.

also, believe may need using meteor's wrapasync method, correct? i've tried (and found this , this relevant questions on it), still can't seem things right.

grateful feedback.

update:

for working approach integrating braintree meteor, check out example repo (many @nick tomlin this)

disclaimer: work braintree :)

i'm not familiar meteor, @mrak noted clienttoken.generate asynchronous , handle appropriately in method.

in current code, clienttoken undefined because console.log(clienttoken) executes immediately, before receive clienttoken callback clienttoken.generate. asynchronous programming can take while wrap head around if used coding in synchronous matter, there many resources out there (here one).

it appears meteor.wrapasync indeed provide need, here untested example implementation.

meteor.methods({   createclienttoken: function() {     var createtoken = meteor.wrapasync(gateway.clienttoken.generate, gateway.clienttoken);      var response = createtoken({});      return response.clienttoken;   } }); 

update

i've created basic braintree + meteor application may of use (if not, please file issue on gh repo improve it!)


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -