Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

callback is not a function node js

I am new to javascript and i am having trouble solving this error. I get the message: "callback is not a function" at:"return callback(rolesArray)".

Rol.getAllRoles = function(callback){
    sql = "select role from Role;";
    var rolesArray = [];
    var role;
    mysql.connection(function(err,conn){
        if (err){
            return callback(err);
        }
        conn.query(sql,function(err,rows){
            if (err){
                return callback(err);
            }
            for(var i=0; i < rows.length; i++){
                role = rows[i].role;
                rolesArray.push(rol);
            }
            console.log("roles: " + rolesArray);
            return callback(rolesArray);
        });
    });   
} 

The console.log outputs:"roles: admin,customer" so the connection with the database works.

like image 979
Reinier Dekker Avatar asked Mar 23 '16 20:03

Reinier Dekker


People also ask

Is callback a function?

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. The above example is a synchronous callback, as it is executed immediately.

What is callback function in node JS?

A callback is a function which is called when a task is completed, thus helps in preventing any kind of blocking and a callback function allows other code to run in the meantime. Callback is called when task get completed and is asynchronous equivalent for a function. Using Callback concept, Node.

Is callback a keyword in node JS?

For your top function, callback is the name of the third argument; it expects this to be a function, and it is provided when the method is called. It's not a language keyword - if you did a "find/replace all" of the word "callback" with "batmanvsuperman", it would still work.

What is JavaScript callback function?

A callback is a function passed as an argument to another function. This technique allows a function to call another function. A callback function can run after another function has finished.


1 Answers

That error means that you are not passing a function to Rol.getAllRoles(fn) when you call it.

In addition, so that you can have proper error handling in your callback and so you can more easily distinguish between an error and the actual data, you should always pass a first argument to the callback that indicates whether there was an error or not and then the second argument (if not an error) can be your results array like this:

Rol.getAllRoles = function(callback){
    sql = "select role from Role;";
    var rolesArray = [];
    var role;
    mysql.connection(function(err,conn){
        if (err){
            return callback(err);
        }
        conn.query(sql,function(err,rows){
            if (err){
                return callback(err);
            }
            for(var i=0; i < rows.length; i++){
                role = rows[i].role;
                rolesArray.push(rol);
            }
            console.log("roles: " + rolesArray);
            // make sure the first argument to the callback
            // is an error value, null if no error
            return callback(null, rolesArray);
        });
    });   
} 

And, then you should be calling it like this:

Rol.getAllRoles(function(err, rolesArray) {
    if (err) {
        // handle error here
    } else {
        // process rolesArray here
    }
});

This style of calling an async callback as in callback(err, data) is a very common async callback design pattern. It allows all callers to see if there was an error or not and if there was no error to get access to the final result.

like image 199
jfriend00 Avatar answered Oct 14 '22 09:10

jfriend00