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

被刻印的时光 ゝ 提交于 2019-12-11 20:37:01

问题


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!


回答1:


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. 
});


来源:https://stackoverflow.com/questions/22276269/balanced-not-getting-a-response-from-marketplace-customers-create-using-node-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!