我一直致力于将Google Recaptcha集成到Meteor和AngularJS Web应用程序中。一切顺利,直到我不得不验证重新回复 - 由于一些奇怪的原因,我无法从后端到前端得到异步响应。
我尝试了很多不同的版本,并且在SO和互联网上阅读了很多很多帖子,但没有运气 - 所以我选择发布自己的问题。
这就是我正在做的事情:
客户端:
Meteor.call('recaptcha.methods.validateRecaptcha', { 'response' : this.recaptcha.getResponse(this.id) }, function(error, result) {
// error and result are both undefined
console.log('Do something with the ' + error + ' or ' + result + '.');
}
所以,我正在调用Meteor方法并传入一个在方法完成时运行的回调。但是,error
和result
参数都未定义。
服务器
run: function(data) {
if (this.isSimulation) {
/*
* Client-side simulations won't have access to any of the
* Meteor.settings.private variables, so we should just stop here.
*/
return;
}
return Meteor.wrapAsync(HTTP.post)(_someUrl, _someOptions);
}
最后一行是我在几个Meteor指南中找到的同步/异步结构的缩短版本(我也试过这个版本),即:
var syncFunc = Meteor.wrapAsync(HTTP.post);
var result = syncFunc(Meteor.settings.private.grecaptcha.verifyUrl, _options);
return result;
我也尝试过使用Futures的版本:
var Future = Npm.require( 'fibers/future' );
var future = new Future();
var callback = future.resolver();
HTTP.post(Meteor.settings.private.grecaptcha.verifyUrl, _options, callback);
return future.wait();
现在,我的意图是我使用Meteor.call()
从客户端调用此方法,客户端存根运行(以防止模拟错误,因为我们在真正的非私有Meteor.settings
变量中使用-SO服务器端代码)并立即返回(发生),服务器点击谷歌的Recaptcha API(发生这种情况,服务器收到响应),然后将结果返回给客户端(这不会发生 - 回调发生但没有错误/成功数据)。
我的想法是发生了两件事之一:
任何流星大师都可以在这里权衡一下,让我知道发生了什么,以及如何在Meteor应用程序中获得非常好的异步请求?
谢谢!
答案 0 :(得分:1)
来自HTTP.call
的{{1}},HTTP.post
的通用版本,它说
可选回调。如果传递,则该方法异步运行,而不是同步运行,并调用asyncCallback。在客户端上,此回调是必需的。
因此,在服务器上,您可以像这样
异步运行它run: function(data) {
if (this.isSimulation) {
/*
* Client-side simulations won't have access to any of the
* Meteor.settings.private variables, so we should just stop here.
*/
return;
}
// No need to pass callback on server.
// Since this part is not executed on client, you can do this
// Or you can use Meteor.isClient to run it asynchronously when the call is from client.
return HTTP.post(Meteor.settings.private.grecaptcha.verifyUrl, _options);
}