dotbat
dotbat

Reputation: 88

Meteor / Javascript function error - TypeError: callback is not a function

I've been looking at other Stack Overflow questions and I still can't figure mine out. I'm getting this error in the browser console:

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

I've put my code below and a comment on the line the error references. It seems that the "err" object isn't getting passed, but the callback is actually being called and the whole thing goes through, it just never catches the error.

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

EDIT: You all got me going in the right direction. Here's the corrected code:

submitForm1(entry, function(err,res){
    processForm1(err,res,entry,function(err,res){
        //Done processing
		console.log(err); 
		console.log(res); 
		console.log("Done");
    })
});

Upvotes: 0

Views: 830

Answers (2)

Yaser
Yaser

Reputation: 5719

You are passing processForm1 as a callback to submitForm1. If you do this you have to make sure that the signature which is called inside processForm1 matches (err, res, entry, callback).

This means that you need to pass a method as callback inside submitForm1 which gets called inside it. Makes sense?

For simplicity let's put it this way:

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

No you see what's wrong.

Upvotes: 1

trincot
trincot

Reputation: 350272

You are calling the function you pass as callback, instead of ... passing it, but then pass another function as argument to that. I think you mixed up two flavours of the same function together in a wrong mix. Fix like this:

submitForm1(entry,
    function (err,res,entry) {
        //Done processing
        console.log(err);
        console.log(res);
        console.log("Done");
    }
)           

Upvotes: 1

Related Questions