[英]Meteor / Javascript function error - TypeError: callback is not a function
我一直在寻找其他Stack Overflow问题,但仍然无法弄清楚。 我在浏览器控制台中收到此错误:
Exception in delivering result of invoking 'formMethod1': TypeError: callback is not a function
我将我的代码放在下面,并在错误引用所在的行上添加了注释。 似乎未传递“ err”对象,但实际上已调用了回调,并且整个过程都进行了,它从未捕获到错误。
submitForm1(entry, processForm1(err,res,entry,function(err,res){ //Done processing console.log(err); //Doesn't work console.log(res); //Doesn't work console.log("Done"); //Works }) ) function submitForm1(entry, callback) { Meteor.call('formMethod1', { params: { user: Meteor.user().username, activity: entry } }, function(err,res){ if(err){ 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) { console.log(err); //Doesn't work console.log(res); //Works 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 console.log(err); console.log(res); console.log("Done"); }) });
您正在调用作为回调传递的函数,而不是...传递它,而是将另一个函数作为参数传递给该回调。 我认为您以错误的方式将具有相同功能的两种口味混合在一起。 像这样修复:
submitForm1(entry,
function (err,res,entry) {
//Done processing
console.log(err);
console.log(res);
console.log("Done");
}
)
你逝去的processForm1
作为回调submitForm1
。 如果这样做,则必须确保在processForm1
内部processForm1
的签名匹配(err, res, entry, callback)
。
这意味着您需要在submitForm1
内部传递一个作为回调的方法,该方法在内部被调用。 说得通?
为简单起见,让我们这样说:
function submitForm1(entry, function(err,res,entry,function(err,res){
//Done processing
console.log(err); //Doesn't work
console.log(res); //Doesn't work
console.log("Done"); //Works
}) {
Meteor.call('formMethod1', {
params: {
user: Meteor.user().username,
activity: entry
}
}, function(err,res){
if(err){
console.log(err) //Works
callback(err, res, entry) //This is where the error happens
} else{
callback(undefined, res, entry)
}
}
);
}
不,你看不出什么问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.