0

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;
1
  • The following post shows some suggestions for how to deal with this situation: stackoverflow.com/questions/18361930/… IMHO the second answer (NOT the accepted answer) provides the cleanest and clearest way to approach the code. Commented Oct 20, 2020 at 18:50

2 Answers 2

0

If you want to use await and block the code from moving further, you have to convert your get all function to return a promise....

getAll() {
    new Promise((resolve,reject) => {
    //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) reject(error);
        const query = (results[0].email);
        console.log(`query is ${query}`);  //THIS PROVIDES THE CORRECT DATA
        resolve(query)  //THIS RETURN IS NOT GETTING BACK TO existingUser variable?
        });
   })
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but I still get undefined being passed back/
Yeah i missed the return. I think it should work after putting the return statement.
return new Promise
0

Promise was the way to go but it just missed the return

getAll() {
    return new Promise((resolve,reject) => {  //ADDING return here fixed it
    //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) reject(error);
        const query = (results[0].email);
        console.log(`query is ${query}`);  //THIS PROVIDES THE CORRECT DATA
        resolve(query)  //THIS RETURN IS NOT GETTING BACK TO existingUser variable?
        });
   })
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.