I have got a node file requesting a return variable from another module page. All the data functions "work" but the "return query" is not sending the query value back to my initial request and my existingUser variable is undefined. In the console logs checks I put in the existingUser ones displays before the query one. It is like the await request is being ignored.
Any help appreciated...and I'm a newbie to all this!
Request page -
const sqlRequests1 = require('./sqlAdmin/sqlRequests1');
.
.
.
app.post('/', async (req, res) => {
const { club, first_name, last_name, email, tel, address, post_code, password, passwordConfirmation} = req.body;
let existingUser = await new sqlRequests1.Queries('contact',email).getAll();
console.log(`existingUser is ${existingUser}`); //THIS CONSOLE LOG RETURNS "undefined"
if (existingUser) {
return res.send('Email in use');
}
if (password !== passwordConfirmation) {
return res.send('Passwords must match');
}
res.send('Account created!!!');
});
Module page - sqlRequests1
class Queries {
constructor(table, selection) {
this.table = table;
this.selection = selection;
console.log(selection); //THIS DATA CHECK WORKS
if(!table) {
throw new Error('Need a table to connect to');
};
};
getAll() {
//Confirm if the emasil exists - if it does then give error message
let q = 'SELECT * FROM ?? ';
connection.query(q, this.table, function (error, results,fields) {
if (error) throw error;
const query = (results[0].email);
console.log(`query is ${query}`); //THIS PROVIDES THE CORRECT DATA
return query; //THIS RETURN IS NOT GETTING BACK TO existingUser variable?
});
};
};
module.exports.Queries = Queries;