【发布时间】:2016-04-13 17:01:21
【问题描述】:
嘿,我在管理 Meteor 应用程序上的错误时遇到了问题,我搜索了这类事情,发现了一些关于同步函数的主题,但我可以解决我的问题。在我的客户端,我调用了一个方法,我需要在通过之前获得结果。 我的客户端:
onSubmit: function(doc){ // Gestion du formulaire d'inscription
var error = null;
var title = doc.title;
var content = doc.content;
var formData = {
title: title,
content: content
};
//get the captcha data
var captchaData = grecaptcha.getResponse();
Meteor.call('createCPDM', formData, captchaData, function(err){
if(err){
error = new Error("Une erreur s'est produite");
}
});
if(error === null){
this.done(); // Appelle onSuccess
}
else{
this.done(error); // Appelle onError
}
// reset the captcha
grecaptcha.reset();
return false;
},
我的服务器端:
createCPDM: function(formData, captchaData) {
var ip = "0.0.0.0";
var verifyCaptchaResponse;
if (!this.connection.clientAddress) {
verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(ip, captchaData);
}
else {
verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
}
if (!verifyCaptchaResponse.success) {
throw new Meteor.Error('422', 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
}
else {
var CPDMID = CPDM.insert(formData);
}
return true;
}
所以在继续之前我需要知道我是否有错误...
感谢您的帮助!!!
【问题讨论】:
-
Javascript 主要是一种 异步 语言,所以将
Meteor.call之后的所有内容放在回调中 -
@Endless 实际上我不能这样做,因为我需要访问
this.done() -
只需将
function(err){}更改为err => {}即可访问this.done -
或做类似
var self = this&self.done()
标签: javascript asynchronous meteor