Unhandledpromiserejectionwarning: Error: Cannot enqueue Query after fatal error

Asked

Viewed 364 times

-1

I have this mistake and I couldn’t find any questions on Stackoverflow that would help me... the error is being generated provable because of a promise

Index.js

const path = require('path')
const app = express()
const port =  process.env.PORT || 3000
const mysql = require('mysql')

const connection = mysql.createConnection({
    host: 'HOST',
    user: 'USER',
    pass: 'PASS',
    database: 'BD' 
})
const dependencies = {
  connection
}

const users = require('./routes/users')

app.use(express.static('public'))

app.get('/', (req, res) => res.render('home'))
app.use('/usuarios', users(dependencies))

//view engine
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')

connection.connect(() => {
  app.listen(port, () => console.log('Servidor no ar, porta:', +port))    
})


Routes/users.js

const express = require('express')
const usersController = require('../controllers/users')

const usersRouter = ({ connection }) => {
    const router = express.Router()     

    router.get('/', usersController.index.bind(null, connection))

    return router

}

module.exports = usersRouter

Controllers/users.js

const users = require('../models/users')

const index = async(connection, req, res) => {
    const results = await users.findAll(connection)
    res.render('olá')
}

module.exports = {
    index
}

Models/users.js

const findAll = (connection) => {
    return new Promise((resolve, reject) => {
        connection.query('select * from users', (err, results) => {
            if(err){
                reject(err)
            }else{
                resolve(results)
            }
        })
    })
}


module.exports = {
    findAll
}

Some kind soul to help me?! kkkkk

  • console.log(Connection) in the findAll method returns what?

  • o node não chega até o fim do método... 
subi o console.log 1 nível e mostrou isso no item _fatalError: { Error: getaddrinfo ENOTFOUND mysql.umbler.com:41890 mysql.umbler.com:41890:3306
 at GetAddrInfoReqWrap.onlookup [as oncomplete]

  • Related: https://answall.com/questions/285695/erro-unhandledpromiserejectionwarning

1 answer

0

Apparently you didn’t connect to the database before running the query.

connection.connect((err) => {
  if(err){
    console.log('não conectado ao mysql', err)
  }else{
    app.listen(port, () => console.log('Servidor no ar, porta:', port))    
  }
})

Important to also check that your connection data is correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.