平衡 - 没有使用节点库从marketplace.customers.create获得响应

时间:2014-03-08 23:09:33

标签: node.js balanced-payments

我正在尝试将Balanced payment集成到节点应用程序中,但出于某种原因,我在创建客户时收到了未定义的响应。然而,客户正在市场中创建。

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

返回customerundefined

到我的控制台。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

balanced.marketplace.customers.create正在后台执行网络调用并返回一个promise,即访问资源上的基础数据,您将不得不使用.then

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

这可能让您感到困惑的原因是平衡节点库使用的承诺是“重载”,因为您可以将操作串在一起。当您想要访问承诺的结果时,您只需使用.then。这意味着您可以执行以下操作:

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