NodeJS promise处于挂起状态,但代码工作正常

bvjxkvbb  于 2023-04-20  发布在  Node.js
关注(0)|答案(1)|浏览(119)
const express = require("express");
const router = express.Router();
const mysql = require('mysql');

const pool = mysql.createPool({
  socketPath:'',
  host: 'localhost',
  user: 'fatima',
  password: 'acess',
  database: 'acess',
  multipleStatements: true,
  canRetry: true,
  connectionLimit : 1000,
  connectTimeout  : 60 * 60 * 1000,
  acquireTimeout  : 60 * 60 * 1000,
  timeout         : 60 * 60 * 1000,
  port: 3000
});

 
let db = {};

db.getUserByEmail = (email) =>{
    return new Promise((resolve, reject)=>{
        pool.query('SELECT * FROM users WHERE email = ?', [email], (error, users)=>{
            if(error){
                return reject(error);
            }
            return resolve(users[0]);
        });
    });
};
 
 

router.post('/login', async (req, res) => {
  
  try{ 
    const email = "sami@mail.com";
    // const password = req.body.Pass;
    const user = await db.getUserByEmail(email);
    console.log("hi",user); } 
    } catch(e){
        console.log(e);
    }

})
[nodemon] restarting due to changes...
[nodemon] starting `node main.js`
listening on port 3000
Database Connected
hi Promise { <pending> }
[nodemon] restarting due to changes...
[nodemon] starting `node main.js`
listening on port 3000
Database Connected
[nodemon] restarting due to changes...
[nodemon] starting `node main.js`
listening on port 3000
Database Connected
[nodemon] restarting due to changes...
[nodemon] starting `node main.js`
listening on port 3000
Database Connected

我的登录路由不正常dirst当我执行我的程序没有异步功能,它显示承诺挂起,但当我使用异步功能,我的程序继续执行,它没有console.log任何东西。
我希望这个解释能帮助理解这个问题。我已经给出了上面所需的代码。
谢谢!

z5btuh9x

z5btuh9x1#

db.getUserByEmail()函数中,尝试使用rejectresolve删除return语句。

相关问题