0

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.

2 Answers 2

2

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")
})
Sign up to request clarification or add additional context in comments.

2 Comments

Ok very good idea thank you very much: i did as you said and it showd me the following error: Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client Do you know what this means?
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.
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.

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.