Meteor / Javascript函数错误 - TypeError:回调不是函数

时间:2016-12-05 21:51:24

标签: javascript meteor

我一直在关注其他Stack Overflow问题,但我仍然无法理解。我在浏览器控制台中收到此错误:

Exception in delivering result of invoking 'formMethod1': TypeError: callback is not a function

我把我的代码放在下面并对错误引用的行进行注释。似乎“错误”对象没有被传递,但回调实际上被调用,整个事情都经过,它只是从来没有捕获错误。

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");
    })
});

2 个答案:

答案 0 :(得分:1)

您正在调用您作为回调传递的函数,而不是...传递它,但是然后将另一个函数作为参数传递给 。我认为你把两种相同功能的风格混合在一起。修复如下:

msi

答案 1 :(得分:1)

您正在将.goal-lower-well { height: 300px; width: 700px; /* margin-top: -42px; */ Remove it position: relative; z-index: 0; } 作为回传传递给processForm1。如果这样做,您必须确保在submitForm1内调用的签名与processForm1匹配。

这意味着您需要在(err, res, entry, callback)内传递一个方法作为回调,并在其中调用。有意义吗?

为简单起见,我们这样说:

submitForm1

不,你看错了。

相关问题