Home > Mobile >  React: Trying to connect index.js to database but its not working
React: Trying to connect index.js to database but its not working

Time:10-11

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.

CodePudding user response:

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")
})

CodePudding user response:

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.

  • Related