0

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

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

返回客户未定义

到我的控制台。

任何帮助,将不胜感激!

4

1 回答 1

3

balanced.marketplace.customers.create后台执行网络调用并返回一个承诺,这意味着访问资源上的底层数据,您将不得不使用.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. 
});
于 2014-03-09T18:47:39.130 回答