我一直在寻找其他堆栈溢出的问题,我仍然无法找到矿井。我得到在浏览器控制台这个错误:流星/ Javascript函数错误 - TypeError:回调不是函数
Exception in delivering result of invoking 'formMethod1': TypeError: callback is not a function
我已经把我下面的代码和注释就行了错误的引用。看起来“err”对象没有被传递,但是回调实际上被调用并且整个事情都经过了,它从来没有捕捉到错误。
submitForm1(entry,
\t processForm1(err,res,entry,function(err,res){
\t \t //Done processing
\t \t console.log(err); //Doesn't work
\t \t console.log(res); //Doesn't work
\t \t console.log("Done"); //Works
})
) \t \t \t
function submitForm1(entry, callback) {
Meteor.call('formMethod1', {
params: {
user: Meteor.user().username,
activity: entry
}
}, function(err,res){
if(err){
\t console.log(err) //Works
callback(err, res, entry) //This is where the error happens
} else{
callback(undefined, res, entry)
}
}
);
}
function processForm1(err, res, entry, callback) {
\t console.log(err); //Doesn't work
\t console.log(res); //Works
\t console.log(entry); //Works
if (err) {
if (err.error == "1001") { //Activity not found
//Handle Error
callback("Activity Not Found");
} else {
//Handle Error
callback(err.message);
}
} else { //No Errors
callback(undefined,"Submitted");
}
}
编辑:你们都让我去在正确的方向。下面是更正后的代码:
submitForm1(entry, function(err,res){
processForm1(err,res,entry,function(err,res){
//Done processing
\t \t console.log(err);
\t \t console.log(res);
\t \t console.log("Done");
})
});
processForm1不返回函数。如果您想调用它并将其结果用作回调函数,则需要它返回一个函数。 –
当您调用submitForm1时,您正在使用两个级别的回调函数,是否因为它使逻辑有点复杂而需要这样做? – Yaser