0

I having a hard time figuring out how to get a value using meteor's onCreateUser. I'm using the coinbase API so things get weird when calling functions. I can't figure out how to save a value to the user object. Here's my code

 Accounts.onCreateUser(function(options, user) {

   Meteor.call('createWallet',user._id);
   var Client = require('coinbase').Client;

   var client = new Client({
      'apiKey': 'API_KEY',
      'apiSecret': 'API_SECRET',
   });

   client.getAccounts({}, function(err, accounts) {

       user.wallet= accounts; //error here
   });

   return user;
});

Now, after doing some research I know that I should call the inner function from the outer, something like

function outer() { 
    function inner() {
        //return value o assign it
    }
    inner(); // call it or assign it to a user.wallet
}

however i dont know how to call the funtcion and the way of calling the functions here is really confusing me. Any guidance on how to solve this would be highly appreciated.

Oscar Or
  • 73
  • 9
  • 1
    Is `client.getAccounts()` asynchronous? – kennasoft May 08 '17 at 22:41
  • //error here ? what is the error exactly? thanks. – Jonathan Dion May 08 '17 at 23:20
  • Assuming `client.getAccounts()` is asynchronous, then you can't return the `user` from the function. That value simply isn't available yet when the function returns. The async function will finish sometime later AFTER your function returns. Instead, you have to either return a promise and use that to get the user or pass in a callback that will be called when the user is available. The question yours is marked a dup explains a lot more and shows multiple options for solving. In a nutshell, you have to code differently for async responses. Can't code serially any more. – jfriend00 May 08 '17 at 23:28

0 Answers0