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

286
Visualizações
React: Trying to connect index.js to database but its not working

So i built a frontend where you can fill in a movie name, a review and submit it to a database. Now im trying to connect a mysql database i created to the index.js , so that it gets filled with the first entry. Im trying to accomplish it like this:

const express = require('express');
const app = express();
const mysql = require('mysql');

const db = mysql.createPool({
    host: "localhost",
    user: "root",
    password:"password",
    database:'CRUDDatabase',
});

app.get('/', (req, res) => {
    const sqlInsert = "INSERT INTO Movie_Reviews(movieName, movieReview) VALUES (1,'inception', 'good movie');" 
    db.query(sqlInsert, (err, result) =>{
        res.send("change done");
    });
})

app.listen(3001, () => {
console.log("running on port 3001")
})

But somehow the frontend gets the text ive send "Change done" but the database still doesnt show any entries. Any ideas where my mistake may be? Is it a code mistake or does it have to do with me db configuration. In mysql workbench i just created a default connection without changing anything.

EDIT: The Error seems to be the following: Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

EDIT: The following comment here solved my problem: Execute the following query in MYSQL Workbench ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; Where root as your user localhost as your URL and password as your password Then run this query to refresh privileges: flush privileges; Try connecting using node after you do so. If that doesn't work, try it without @'localhost' part.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I think you have an error in your code but you are not showing it as you don't test in err variable, try this code in order to show what error you are getting:

const express = require('express');
const app = express();
const mysql = require('mysql');

const db = mysql.createPool({
  host: "localhost",
  user: "root",
  password:"password",
  database:'CRUDDatabase',
});

app.get('/', (req, res) => {
  const sqlInsert = "INSERT INTO Movie_Reviews(movieName, movieReview)       VALUES (1,'inception', 'good movie');" 
  db.query(sqlInsert, (err, result) =>{
    if(err) {
       console.log(err);
       res.send(err.toString()); 
    }
    res.send("change done");
  });
})

app.listen(3001, () => {
  console.log("running on port 3001")
})
about 4 years ago · Juan Pablo Isaza Relatório

0

So as Med Amine Bejaoui pointed out in a comment, the solution is:

Execute the following query in MYSQL Workbench ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; Where root as your user localhost as your URL and password as your password Then run this query to refresh privileges: flush privileges; Try connecting using node after you do so. If that doesn't work, try it without @'localhost' part.

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