Meteor方法对客户端返回undefined(asynchronous)
我一直在努力将Google Recaptcha整合到Meteor和AngularJS Web应用程序中。 一切都很顺利,直到我必须validationrecaptcha响应 – 由于一些奇怪的原因,我无法从后端到前端获得asynchronous响应。
我已经尝试了很多不同的变体,并且在SO和互联网上阅读了很多很多的post,但没有运气 – 所以我select发布自己的问题。
这是我正在做的事情:
客户:
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方法,并传入一个方法完成后运行的callback函数。 但是, 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指南(我也试过这个版本)中find的同步/asynchronous结构的缩短版本,即:
var syncFunc = Meteor.wrapAsync(HTTP.post); var result = syncFunc(Meteor.settings.private.grecaptcha.verifyUrl, _options); return result;
我也尝试过使用期货的版本:
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()
从客户端调用此方法,客户端存根运行(以防止模拟错误,因为我们在真正的非SO服务器端使用私有的Meteor.settings
variables代码)并立即返回(发生这种情况),服务器在返回结果给客户端之前碰到Google的Recaptcha API(发生这种情况,服务器接收到响应)(这种情况没有发生 – callback发生但没有错误/成功数据)。
我的想法是,有两件事情正在发生:
- 我只是做错了,我不正确地发送数据回到客户端。
- 同步客户端存根(立即返回)告诉客户端,服务器响应并不重要,所以它永远不会等待正确的asynchronous响应。
任何meteor大师都可以在这里权衡一下,让我知道发生了什么事,以及如何获得asynchronous请求在Meteor应用程序中很好地播放?
谢谢!
从HTTP.call
的文档 ,这是HTTP.call
的通用版本,它说
可选的callback。 如果通过,方法asynchronous运行,而不是同步,并调用asyncCallback。 在客户端,这个callback是必需的。
所以,在服务器上,你可以像这样asynchronous地运行它
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); }