0

Trying to connect to a local mysql database through an index.js file for a very simple react & node/express app I am building for practice. But I can't seem to connect to my index.js file to my mysql database.

I am pretty sure all dependancies have been installed but I have been at this for hours!

It appears that I can manually input new records using terminal to the relevant band_reviews table.

mysql> INSERT INTO band_reviews (bandName, bandReview) VALUES ('Lofi','Really cool');
Query OK, 1 row affected (0.01 sec)

+----+-----------+-------------+
| id | bandName  | bandReview  |
+----+-----------+-------------+
|  1 | Coldfire  | Awesome     |
|  2 | Romatico  | Bad         |
|  3 | Pizza     | Great       |
|  4 | Metallica | Really cool |
|  5 | Lofi      | Really cool |
+----+-----------+-------------+

Yet, I can't seem to connect to this DB using my index.js file with the below configurations.

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


const db = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "band_reviews",
});

db.connect((err) => {
    if(err){
        console.log('Error connecting to Db');
    } else { 
        console.log('Connection Established');
    }   
});

app.get("/", (req,res) => {
    const sql = "INSERT INTO band_reviews (bandName, bandReview) VALUES ('Owane','The Best');";
    db.query(sql, (err, result) => {
        if (!err) {
            console.log('Successfully added information.');
            } else {
            console.log('Was not able to add information to database.');
        }
        res.send("Testing DB Connection");
    });
});

db.end(function (err){
    //
});

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

package.json

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "devStart": "nodemon index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "mysql": "^2.18.1",
    "nodemon": "^2.0.15"
  }
}

Thanks for your time!

UPDATE: I managed to connect my DB after following this post.

But am still not successfully delivering a record into the desired table.

Ilia
  • 43
  • 9
  • is the name of the database same as the name of the table - 'band_reviews'? – Venkatesh A Nov 28 '21 at 19:38
  • what exactly is the error/issue with the insert function? – Venkatesh A Nov 28 '21 at 19:39
  • @VenkateshA - that's right, also is the name of the DB... so table and DB named band_reviews – Ilia Nov 29 '21 at 14:57
  • @VenkateshA - I can't tell at this point, I feel like everything here is correct, yet the insert does not fire when i visit "/" ... I don't know if I am missing certain details, dependencies... my connection is successful but the INSERT query results in 'Was not able to add information to database.' – Ilia Nov 29 '21 at 15:01

0 Answers0