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...