Saturday 13 May 2017

node.js how to pass return callback from file to other

i created a callback to use return on this function on file

move.js

    class move{ 

    getLastMove(id,callback){


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

        var query = connection.query(MoveRequest, function(err,rows, result) {
        if(err) {
             return callback(err)
        }

        //console.log('rows', rows.length);
        if (rows.length == 0) { // evaluate the count
            return callback(null,"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(null,move);
                }

            }
        }



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

        });


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


    }

    }

exports.move = move;

and to call it on other file auth.js i added this lines

const Move = require('./move.js');
var a = new Move.move();

calling function line i write it like that

  a.getLastMove(req.user.GameId);

the full code on the auth.js where i want to call the call back

app.post('/dispalymove', function (req, res, next) {
var lMove="";


if(req.body.MoveString !== null){
     Move.setMoveUserId(req.user.id);
     Move.setMoveString(req.body.MoveString);
     lMove = a.getLastMove(req.user.GameId);
     console.log('gameid:',req.user.GameId);

    }
     var output = {"msg":lMove, "loggedin":"true"};

     res.send(JSON.stringify(output));



});

but when I run this code i get this error

TypeError: callback is not a function

because this line

        callback(null,move);

on php I can simply do that

auth.php file

session_start();

    include "/move.php";
    $lMove="";
    if(isset($_POST['MoveString'])){
        $move = new move();
        $move->setMoveUserId($_SESSION['UserId']);
        $move->setMoveString($_POST['MoveString']);
        $lMove=$move->getLastMove($_SESSION['GameId']);
    }
    $output = array("msg"=>"$lMove", "loggedin"=>"true");
    echo json_encode($output);

and on move.php

class move {

    public function getLastMove($id){
        include "../../connectToDB.php";
        $MoveRequest=$_db->query("SELECT * FROM users ORDER BY UserId");
        $existCount = $MoveRequest->rowCount();

        if ($existCount == 0) { // evaluate the count
            return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
        }
        if ($existCount > 0) {
            while($row = $MoveRequest->fetch(PDO::FETCH_ASSOC)){
                $gameID = $row["GameId"];   
                $move = $row["MoveString"];                 
                if($gameID == $id) {
                    return $move;

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



};



via dark night

No comments:

Post a Comment