0

for some reason the Promise in node is not working although it worked before. this is my express route:

    app.post('/login/', function (req, res) {
    let email       =   req.body.email;
    let password    =   req.body.password;
    let companyId   =   req.body.companyId;
    let controller  =   require('./controllers/agents/index');
    controller.login(email, password, companyId).then((data) => {
        console.log(data)
        res.send(data);
    });

it calls a login function which execute sql query:

login(email, password, companyId) {
    let loginPromise  =   new Promise(function(resolve, reject) {
        email       =   mysql.escape(email);
        password    =   mysql.escape(password);
        companyId   =   mysql.escape(companyId);
        if(!email || !password || !companyId) {
            let data    =   {
                code: 401,
                test: 'Missing Parameter'
            }
            resolve(data)
        }
        let dataReturn    =   {};
        let query   =   `SELECT agents.id FROM buschat.agents WHERE email=${email} AND password = ${password} AND company_id = ${companyId}`;
        DB.Q(query).then((data) => {
            if(data.length) {
                dataReturn    =   {
                    code: 200,
                    text: 'Login Success.'
                }
            } else {
                dataReturn    =   {
                    code: 400,
                    text: 'Login Failed.'
                }
            }
            console.log(dataReturn);
            resolve(data);
        });
        resolve({})
    });
    return loginPromise;
}

as you can see there is a console log there which show great the object but for some reason the resolve function not sending it to the route...

Paz Lazar
  • 229
  • 5
  • 14
  • Why are you attempting to call `resolve` twice? Also, avoid the [`Promise` constructor antipattern](http://stackoverflow.com/q/23803743/1048572)! – Bergi Mar 24 '16 at 13:40
  • i removed the second resolve. still its not sending the data from the promise to "then". – Paz Lazar Mar 24 '16 at 13:45
  • So you claim you're getting the first `console.log` but not the second one? – Bergi Mar 24 '16 at 13:48
  • yes but i found the problem. i return "data" which is also the callback from the DB.Q return object... – Paz Lazar Mar 24 '16 at 13:52

0 Answers0