I';如果我使用Meteor,我需要做什么来等待API调用返回承诺?
我只是使用了默认的hello world,没有问候语行I';如果我使用Meteor,我需要做什么来等待API调用返回承诺?,meteor,balanced-payments,Meteor,Balanced Payments,我只是使用了默认的hello world,没有问候语行 if (Meteor.isClient) { Template.hello.events({ 'click input': function () { //create a new customer Meteor.call('createCustomer', function (error, result) { console.log("Error: " + error + " R
if (Meteor.isClient) {
Template.hello.events({
'click input': function () {
//create a new customer
Meteor.call('createCustomer', function (error, result) {
console.log("Error: " + error + " Result: " + result); } );
}
});
}
if (Meteor.isServer) {
Meteor.methods({
createCustomer: function () {
try {
balanced.configure('MyBalancedPaymentsTestKey');
var customer = Meteor._wrapAsync(balanced.marketplace.customers.create());
var callCustomer = customer();
var returnThis = console.log(JSON.stringify(callCustomer, false, 4));
return returnThis;
} catch (e) {
console.log(e);
var caughtFault = JSON.stringify(e, false, 4);
}
return caughtFault;
}
});
}
测试用例
{{>你好}
你好,世界!
在客户端打印日志
错误:未定义的结果:{}
在服务器端打印日志
[TypeError:Object[Object Promise]没有方法“apply”]
你知道我怎么能等待那个承诺而不是返回空白的结果吗? 更新此行
<head>
<title>testCase</title>
</head>
<body>
{{> hello}}
</body>
<template name="hello">
<h1>Hello World!</h1>
<input type="button" value="Click" />
</template>
更新此行
<head>
<title>testCase</title>
</head>
<body>
{{> hello}}
</body>
<template name="hello">
<h1>Hello World!</h1>
<input type="button" value="Click" />
</template>
另一种方法是使用期货。我在服务器端经常使用它来等待结果返回到客户端 以下是我用于登录的一个小示例:
var customer = Meteor._wrapAsync(balanced.marketplace.customer.create)();
另一种方法是使用期货。我在服务器端经常使用它来等待结果返回到客户端 以下是我用于登录的一个小示例:
var customer = Meteor._wrapAsync(balanced.marketplace.customer.create)();
我假设
balanced.marketplace.customers.create
返回承诺/a+承诺。这是一个方法为的对象。然后(fulfillmentCallback,rejectionCallback)
-当操作成功时调用fulfillmentCallback
,如果操作出错,则调用rejectionCallback
。以下是如何使用期货同步获得承诺的价值:
var Future=Npm.require(“光纤/未来”);
功能从承诺(承诺){
var fut=新未来();
承诺。然后(功能(结果){
fut[“返回”](结果);
},函数(错误){
fut[“throw”](错误);
});
返回fut.wait();
}
然后,您只需调用balanced.marketplace.customers.create
(无\u wrapAsync
)即可获得承诺,然后调用该承诺的extractFromPromise
即可获得实际结果值。如果出现错误,则extractFromPromise
将引发异常
顺便说一下,
if(Meteor.isServer)
块中的代码仍然被发送到客户端(即使客户端没有运行),因此您不想将API密钥放在那里。您可以将代码放在服务器
目录中,然后Meteor根本不会将其发送给客户端。我假设balanced.marketplace.customers.create
返回承诺/a+承诺。这是一个方法为的对象。然后(fulfillmentCallback,rejectionCallback)
-当操作成功时调用fulfillmentCallback
,如果操作出错,则调用rejectionCallback
。以下是如何使用期货同步获得承诺的价值:
var Future=Npm.require(“光纤/未来”);
功能从承诺(承诺){
var fut=新未来();
承诺。然后(功能(结果){
fut[“返回”](结果);
},函数(错误){
fut[“throw”](错误);
});
返回fut.wait();
}
然后,您只需调用balanced.marketplace.customers.create
(无\u wrapAsync
)即可获得承诺,然后调用该承诺的extractFromPromise
即可获得实际结果值。如果出现错误,则extractFromPromise
将引发异常
顺便说一下,
if(Meteor.isServer)
块中的代码仍然被发送到客户端(即使客户端没有运行),因此您不想将API密钥放在那里。您可以将代码放入服务器
目录,然后Meteor根本不会将其发送到客户端。更新此行var customer=Meteor.\u wrapAsync(balanced.marketplace.customer.create)()代码>更新此行var customer=Meteor.\u wrapAsync(balanced.marketplace.customer.create)()代码>当我更改该行时,客户端和服务器端都没有任何结果。我甚至在方法中的每个步骤之间添加了控制台日志,但在服务器或客户端的控制台上没有打印任何内容…请显示代码:balanced.marketplace.customers.create
define我正在使用包平衡支付产品,它包装了balanced官方节点包,这样我就可以在代码中的任何地方调用balanced。为了确保在我更改该行时可以查看节点包,我在客户端和服务器端都没有得到任何结果。我甚至在方法中的每个步骤之间添加了控制台日志,但在服务器或客户端的控制台上没有打印任何内容…请显示代码:balanced.marketplace.customers.create
define我正在使用包平衡支付产品,它包装了balanced官方节点包,这样我就可以在代码中的任何地方调用balanced。为了确保您可以查看node包Ok,我以前尝试过使用futures。我不知道如何写我的函数。我是这样重写的,但我知道这是不对的<代码>createCustomerFuture:function(){var Future=Npm.require(“fibers/Future”);var fut=new Future();function runThisHere(function(err,result){balanced.configure('TESTKEYHERE');balanced.marketplace.customer.create();});return fut.wait();}
我稍微更改了代码,现在得到的结果与赵友发布的另一个解决方案相同<代码>createCustomerFuture:function(){var Future=Npm.require(“fibers/Future”);var fut=new Future();function runThisHere(err,result){balanced.configure('TESTKEYHERE');var customer=balanced.marketplace.customer.create();console.log(customer);};return fut.wait();}
这很难理解(不确定stackoverflow为什么不允许带有代码的注释,但这是一个单独的问题),但我明白了要点。假设要将客户返回到调用函数,则需要在运行thishere
函数中放入fut.return(customer)
。嗯。我加了那个,但我仍然没有得到任何回应。我现在有了_wrapAsync、futures和Async.wrap函数。我