Saturday, 13 May 2017

node.js using callback on class functions

I want to return value to varibales so I tried to write it like that using return but that did't work

class move {
    getLastMove(id){


        var MoveRequest = "SELECT * FROM users ORDER BY id";

        var query = connection.query(MoveRequest, function(err,rows, result) {
        //console.log('rows', rows.length);
        if (rows.length == 0) { // evaluate the count
            return ("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
        }
        if (rows.length > 0) {
            for (var i in rows) {


                console.log('getLastMove',id);
                var move = rows[i].MoveString; 
                if (rows[i].GameId == id){

                    return move;
                }

            }
        }



        //console.log("Total Records:- " + result[0].total);

        });


            var move="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
            return move;


    }


};

so I tried to use callback like that

class move {

        getLastMove(id){


            var MoveRequest = "SELECT * FROM users ORDER BY id";

            var query = connection.query(MoveRequest, function(err,rows, result,callback) {
            //console.log('rows', rows.length);
            if (rows.length == 0) { // evaluate the count
                callback ("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
            }
            if (rows.length > 0) {
                for (var i in rows) {


                    console.log('getLastMove',id);
                    var move = rows[i].MoveString; 
                    if (rows[i].GameId == id){

                        callback(move);
                    }

                }
            }



            //console.log("Total Records:- " + result[0].total);

            });


                var move="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
                callback(move);


        }


    };

but when run callback example i get this error

TypeError: callback is not a function



via dark night

No comments:

Post a Comment