Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry

JS async function returning undefined although calling it inside an asynchronous function in node.js ?

JS async function returning undefined although calling it inside an asynchronous function in node.js ?

You have to return some value from your getUserInfo function

If connection query doesn't support promise you should wrap it like this

 /* get user info */
const getUserInfo = async(req) => {
  let userID = req.cookies.userId;
  if (!userID) return null;
  return new Promise((resolve, reject) => {
    connection.query('SELECT * FROM users WHERE id = ' + userID,
      function(err, rows, fields) {
        if (err) {
          reject(err)
          return;
        }
        // if user not found
        if (rows.length === 0) {
          resolve(null)
        }
        // if user found
        else {
          resolve(rows[0]);
        }
      });
  });
}

6 0

Write a Comments


* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online