Search code examples
node.jsbalanced-payments

Balanced - not getting a response from marketplace.customers.create using node library


I'm trying to integrate Balanced payments into a node app but for some reason I'm getting an undefined response when creating a customer. The customer is being created in the marketplace, however.

    balanced.configure('ak-test-2dE1FyvrskNw4o7CiAsGvYOgD7aPSb0ww');    
    var customer = balanced.marketplace.customers.create({
         email: userAccount.emails[0].address,
         name: userAccount.username
    });
    console.log('customer' + customer.ID);

Returns customerundefined

To my console.

Any help would be appreciated!


Solution

  • The balanced.marketplace.customers.create is preforming a network call in the background and returning a promise, which means to access the underlying data on the resource, you are going to have to use .then

    var customer = balanced.marketplace.customers.create(...);
    customer.then(function(c) {
        console.log(c.href);
    });
    

    The reason this might have confused you is that the promise that the balanced node library uses is "overloaded," in that you can string actions together. You only have to use .then when you want to access a result of the promise. This means you can do something like the following:

    var card = balanced.get('/cards/CCasdfadsf'); // this is a network call
    var customer = balanced.marketplace.customers.create(); // this is a network call that will go in parallel
    card.associate_to_customer(customer).debit(5000); // using promises these will complete once all the previous request are complete
    customer.then(function (c) {
        console.log(c.href); // since we need to access the data (href) on the customer, we have to wait for the non blocking requests to complete and then the data will be ready. 
    });