0

I am currently developing signup page and I want to check if email address is already exist in the database.

var emailnum = email_num(`select * from contactinfo where email='${email}'`);
        console.log(emailnum); //the output presents Promise { <pending> } not a num.

function sqlExecute(q) {
    return new Promise((resolve, reject) => {
            db.pool.query(q, function(err, result) {
                if (err) {
                    console.log("ERROR ON " + q+"\n");
                    reject(err)
                }
                console.log("SUCCESS ON " + q);
                resolve(result);
            })
        })
        .catch(err => {
            console.log(err);
        })
}

//run check sql
async function email_num(tempquery){
   var result = await sqlExecute(tempquery);
   return result.rowCount;
}

I tried multiple ways but still could not figure it out. I would appreciate any help TT

when I console.log, output is Always Promise { }. I tried

var emailnum = email_num(`select count(*) as count from contactinfo where email='${email}'`)
.then((val)=>{
return val
};
console.log("number"+ emailnum);

2 Answers 2

1

The problem here is that you're not allowing the promise to resolve before attempting to retrieve the row count. When you create a function using async the return result is always going to be a promise. Here are a few solutions that will get you your desired result:

Solution 1: Use console.log to print the result of the promise after it has been resolved.

const email = '[email protected]'
const numOfEmailsQuery = `SELECT * FROM contactinfo WHERE email = '${email}'`

email_num(numOfEmailsQuery)
  .then(console.log)
  .catch(console.error)

Solution 2: Use await inside of an async function to resolve the result of the promise and store it in a variable. Then print the result using console.log

async function printNumberOfEmails(email) {
  const numOfEmailsQuery = `SELECT * FROM contactinfo WHERE email = '${email}'`
  try {
    const numOfEmails = await email_num(numOfEmailsQuery)
    console.log(numOfEmails)
  } catch (err) {
    console.error(err)
  }
}

const email = '[email protected]'
printNumberOfEmails(email)

Hope that helps! Good luck!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the answer! I tried both solution but still gives me Promise { <pending> }. I want to retry solution 2, and it doesn't need to return numOfEmails?
0

First off, it's good practice to use prepared statements instead.

Also, you should just care about the count, so instead of select *, do select count(*) as count. Then you'll need check either the number of rows that result returned or the first index of rows, then the count property of that.

It should look something like this

function fetchEmailExists(email) {
    const statement = 'select count(*) as count from contactinfo where email = $1';
    return new Promise((resolve, reject) => {
            db.pool.query(statement, [ email ], function(error, result) {
                if (!error && result.rows.length > 0) {
                    resolve(result.rows[0]['count'] > 0);
                } else {
                    reject(error);
                }
            });
        })
        .catch(error => {
            console.log(error);
        });
}

// Here is your call to fetchEmailExists
fetchEmailExists('[email protected]')
  .then(boolean => console.log(boolean));

2 Comments

Thank you very much for the answer! I tried the function with new query, when I console.log it still gives me Promise { <pending> }.
Where ever you're calling fetchEmailExists you have to call then on the promise.

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.