0

I have been working this project for a week, and I can't find any solution using Promise.js, Q.js, Async.waterfall but I can't get my desired out put.

This code are written in node.js I'm trying to check a CHARACTER if it is in the case (switch-case) if true the program will check the name and other info in Mysql database with a asynchronous function.

If the data existing in the database the program will assign true else false.

Now, the real problem is, if I input a correct value the function returns false.

here is my sample code

function checkPosCan(watcherid, message, cpnumber, json){
var correct = false;
var len = json.data.length;
for (var i = 0; i < len; i++) {
    var fn = (json.data[i].cfname).toLowerCase();
    var ln = (json.data[i].clname).toLowerCase();
    switch((json.data[i].cpos).toLowerCase()){
        case "g":
            getCandiName(fn, ln, "governor", function(dataa){
                console.log("count ", dataa.count, " id ", dataa.id);
                if(dataa.count != 1){
                    correct = false;
                    insertLogMessage(watcherid, message, cpnumber, json);
                    console.log("false sa g part");
                } else {
                    correct = true;
                    console.log("true sa g part");
                }
            });
            break;
        case "vg":
            getCandiName(fn, ln, "vice_governor", function(dataa){
                //console.log("data " , dataa.count, dataa.id);
                if(dataa.count != 1){
                    correct = false;
                    insertLogMessage(watcherid, message, cpnumber, json);
                } else {
                    correct = true;
                }
            });
            break;
        case "m":
            getCandiName(fn, ln, "mayor", function(dataa){
                //console.log("data " , dataa.count, dataa.id);
                if(dataa.count != 1){
                    correct = false;
                    insertLogMessage(watcherid, message, cpnumber, json);
                } else {
                    correct = true;
                }
            });
            break;
        case "vm":
            getCandiName(fn, ln,  "vice_mayor", function(dataa){
                //console.log("data " , dataa.count, dataa.id);
                if(dataa.count != 1){
                    correct = false;
                    insertLogMessage(watcherid, message, cpnumber, json);
                } else {
                    correct = true;
                }
            });
            break;
        case "bm":
            getCandiName(fn, ln, "board_member", function(dataa){
                //console.log("data " , dataa.count, dataa.id);
                if(dataa.count != 1){
                    correct = false;
                    insertLogMessage(watcherid, message, cpnumber, json);
                } else {
                    correct = true;
                }
            });
            break;
        case "cg":
        case "cm":
        case "cw":
            getCandiName(fn, ln, "congressman/woman", function(dataa){
                //console.log("data " , dataa.count, dataa.id);
                if(dataa.count != 1){
                    correct = false;
                    insertLogMessage(watcherid, message, cpnumber, json);
                } else {
                    correct = true;
                }
            });
            break;
        case "c":
            getCandiName(fn, ln, "councilor", function(dataa){
                //console.log("data " , dataa.count, dataa.id);
                if(dataa.count != 1){
                    correct = false;
                    insertLogMessage(watcherid, message, cpnumber, json);
                } else {
                    correct = true;
                }
            });
            break;
        default:
             pool.getConnection(function(err, con){
                /** invalid format **/
                con.query("insert into data_log (pw_party_id, message, created_date_stamp, status, status_date, remarks) VALUES (?,?,?,?,?,?)", [watcherid, message, dateFormat(now, "yyyy-mm-dd HH:MM:ss"), "rejected", dateFormat(now, "yyyy-mm-dd HH:MM:ss"), "err_code_03: This number " + cpnumber + " sends an invalid message format (positions). Message is REJECTED."] , function(err){
                    console.log(err);
                });
                 correct = false;
            });
    }
}
console.log("log from inside the function ", correct);
return correct;
}

and to call

console.log(checkPosCan(watcherid, message, cpnumber, json));

Can someone help me or give me sample codes simillar to my problem? I already lose 2 nights without sleeping.

UPDATE

I already got the answer here:

Click link

credits to @Felix Kling

Community
  • 1
  • 1
something
  • 99
  • 12
  • helpp............................. – something Mar 01 '16 at 16:48
  • 1
    You cannot synchronously return a value obtained asynchronously. You will have to restructure your code to use callbacks. Plenty of examples are provided in the question this is marked a dup of. – jfriend00 Mar 01 '16 at 17:24

0 Answers0