I am new to node.js and I want to use promises on my school project. I have found several things online and on stack : Use promise to process MySQL return value in node.js
But I have a question, so far, this is what I have :
router.post('/matchaSearch', function(req, res) {
var username = session.uniqueID;
var searcherPackage = {};
function userAgeCheck(randomParam) {
return new Promise((resolve, reject) => {
pool.getConnection((err, connection) => {
var query = 'SELECT username, age, orientation, sex FROM usersinfo WHERE username != ?';
connection.query(query, [username], (err, rows, fields) => {
connection.release();
return err ? reject(err) : resolve(rows);
});
});
});
}
userAgeCheck('username')
.then((rows) => {
/*console.log(rows);*/
searcherPackage = rows;
console.log(searcherPackage);
// do stuff
}).catch((err) => {
throw err;
});
});
This is working for me, it returns me everything from the db except me (username). But for me this is not right. Why giving a random param is good ?
So I should give 'username' as param instead of 'randomParam' but if I do it, the query will return me everything instead of everyone except me (username). So I got rid of it and just gave it 'randomParam' and it worked. Can you explain this ? Am I doing this right ? If so, i can keep doing my project. Thank you very much for any help !