Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

494
Visualizações
Is there a way of solving this typeError: connection.connect is not a function in nodejs?

I am trying to connect my school project to a mysql database but I always get the error "TypeError: connection.connect is not a function". I am not quite sure how to fix it. I hope someone is able to solve this.

My index.js

const express = require('express');
const app = express();
var connection = require('./database');

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

app.get('/form', (req,res) =>{
    res.sendFile(__dirname + '/public/index.html' );
    console.log(req.url);
    console.log(req.path);
})

app.listen(4000, () =>{
    console.log("Server listening on port 4000");
    connection.connect((err) => {
        if(err) throw err;
        console.log("Connected");
    })
});

my database.js

let mysql = require('mysql');

module.exports = () => {
    return mysql.createConnection({
    host: 'localhost',
    database: 'minigames',
    user: 'root',
    password: 'root'
})}```
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

connection is the value exported from the database module.

The value you assigned to module.exports there is a function that you defined.

It is a plain, ordinary function and you have no assigned a connect property to it.

I'm guessing that connect is a property on the return value of mysql.createConnection, but if you want to access that object then, you need to call the function you defined in order to get that object.

about 4 years ago · Juan Pablo Isaza Relatório

0

const express = require('express');
const app = express();
var connection = require('./database');


app.listen(4000, () =>{
    console.log("Server listening on port 4000");
    connection().connect((err) => {
        if(err) throw err;
        console.log("Connected");
    })
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda