Javascript Meteor方法将未定义的返回给客户端(异步)
我一直致力于将Google Recaptcha集成到Meteor和AngularJS web应用程序中。在我不得不验证recaptcha响应之前,一切都很顺利——出于一些奇怪的原因,我无法从后端到前端获得异步响应 我尝试了很多不同的方法,在SO和互联网上读了很多很多帖子,但是运气不好,所以我选择发表我自己的问题Javascript Meteor方法将未定义的返回给客户端(异步),javascript,angularjs,node.js,asynchronous,meteor,Javascript,Angularjs,Node.js,Asynchronous,Meteor,我一直致力于将Google Recaptcha集成到Meteor和AngularJS web应用程序中。在我不得不验证recaptcha响应之前,一切都很顺利——出于一些奇怪的原因,我无法从后端到前端获得异步响应 我尝试了很多不同的方法,在SO和互联网上读了很多很多帖子,但是运气不好,所以我选择发表我自己的问题 以下是我正在做的: 客户端: Meteor.call('recaptcha.methods.validateRecaptcha', { 'response' : this.recapt
以下是我正在做的: 客户端:
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 + '.');
}
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方法并传入一个回调,该回调在方法完成时运行。但是,错误
和结果
参数均未定义
服务器:
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 + '.');
}
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指南中找到的sync/async结构的缩短版本(我也尝试过这个版本),即:
我还尝试了使用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()
从客户端调用此方法,客户端存根运行(因为我们在真正的非SO服务器端代码中使用私有Meteor.settings
变量),并立即返回(发生这种情况),服务器点击Google的Recaptcha API在将结果返回给客户端之前(这不会发生——回调发生,但没有错误/成功数据)
我认为有两件事正在发生:
HTTP.call
,它是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);
}
感谢您的回复。这实际上是我第一次尝试让它工作(在所有的
Meteor.wrapAsync()
和期货业务之前)--但客户端仍然没有收到错误/成功数据。@SamuelMS:当您在服务器上记录结果时,您在服务器控制台中看到了什么。假设您想在服务器和控制台上异步执行它。在回调中记录,您得到了错误或结果吗?从我所看到的,您的wrapAsync
和Future
的内容也正确。我只能想到HTTP.post
成功地被调用,但没有数据返回给您。我从HTTP收到一个响应——服务器正确地发布并收到预期的结果(成功/失败,有适当的输入)。这似乎真的是一个将结果返回给客户的问题。不用担心,谢谢你花时间研究这个问题。我特别感谢你对futures/wrapAsync语法的反馈,因为更多的人关注它会减少它只是一个简单错误的机会。好吧,这很尴尬——在休息一下,用f回来之后resh eyes,看起来你的回答真的一针见血。我在服务器上传递回调,因为我也想在那里做额外的工作,如果你想客户端回调也运行,你就不能这样做。我的最终解决方案使用futures来包装HTTP回调,并在所有额外工作完成后解决它的承诺。再次感谢!您能在服务器端发布完整的Meteor方法吗?它不仅仅是run:function(data){…}