2

I wanna return the MySQL query in Node.js, but I got some problems.

Prob1. 'var userInfo' cannot get the value from function 'Authenticate()'

Prob2. The throw will be catched by 'dbclient.query', not my code you can see.

Hope guys can help me.


app.post('/create_member_check', function(req, res) {
    var Authenticate = function () {
        SearchUser(req.body.email, function (isExist) { 
            if (isExist === true)
                throw 101;
            else if (req.body.email === undefined || req.body.email == "")
                throw 102;
            else if (req.body.password === undefined || req.body.password == "")
                throw 103;
            else if (isExist === undefined)
                throw 104;

            var user = {
                "email": req.body.email,
                "password": req.body.password
            };
            AddUser(user);
            // This line cannot return the 'user' for 'Authenticate()' caller.
            return user;
        });
    }

    try {
        var userInfo = Authenticate();
    }
    catch (err) {
        var userInfo;
        if (err == 101)
            userInfo = "[Error] This account already exists.";
        else if (err == 102)
            userInfo = "[Error] Please key in 'email'.";
        else if (err == 103)
            userInfo = "[Error] Please key in 'password'.";
        else if (err == 104)
            userInfo = "[Fatal Error] SearchUser return 'undefined'.";
    }

    res.render("login_system/create_member_check", {
        layout: false,
        pagename: "create",
        authenticate: userInfo
    });
});

SearchUser = function (email, callback) {
    dbclient.query("SELECT * FROM user WHERE email = \"" + email + "\"", function (err, results) {
        if (err || results.length <= 0)
            callback(false);
        else
            callback(true);
    });
}

1 Answer 1

1

Authenticate method can't be synchronous. You should make asynchronous method. Try this.

app.post('/create_member_check', function(req, res) {
    var Authenticate = function (req, callback) {
        SearchUser(req.body.email, function (isExist) { 
            if (isExist === true)
                return callback(101);
            else if (req.body.email === undefined || req.body.email == "")
                return callback(102);
            else if (req.body.password === undefined || req.body.password == "")
                return callback(103);
            else if (isExist === undefined)
                return callback(104);

            var user = {
                "email": req.body.email,
                "password": req.body.password
            };
            AddUser(user); //this is maybe asynchronous, again
            callback(null, user);
        });
    }

    Authenticate(req, function(err, user){
        var userInfo;
        if (err == 101)
            userInfo = "[Error] This account already exists.";
        else if (err == 102)
            userInfo = "[Error] Please key in 'email'.";
        else if (err == 103)
            userInfo = "[Error] Please key in 'password'.";
        else if (err == 104)
            userInfo = "[Fatal Error] SearchUser return 'undefined'.";

        res.render("login_system/create_member_check", {
            layout: false,
            pagename: "create",
            authenticate: userInfo
        });
    });
});

And read this article ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.