items does not add to mySQL Table - mysql

Do you know why I can not manually add the value "Sasan" to mySQL server?
MarioDB is installed in PHPStorm and schema is correctly selcted.
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const mysql = require("mysql");
const cors = require("cors");
const db = mysql.createPool({
host: "localhost",
user: "root",
password: "Test123456",
database: "shoppingList-DB"
});
app.use(cors());
app.use(express.json());
app.use(bodyParser.urlencoded({extended: true}));
app.get("/test", (req, res) => {
const sqlInsert = "INSERT INTO shoppingList (itemList) VALUES ('Sasan')";
db.query(sqlInsert, (error, result) => {
console.log("error", error);
console.log("result", result)
res.send("Hello Express");
})
})
app.listen(5001, () =>{
console.log("Server is up - Port 5001");
})

The error is pretty clear Access denied for user 'root'#'localhost'. Check your credentials and user access in mysql.user table.

Related

Connecting to Database using Node & Express

I’m new to node.js and just tried to follow some tutorial. But I get nothing after I sent this GET request.
I used following code.
const express = require('express');
const app = express();
const mysql = require('mysql');
const db = mysql.createConnection({
user: "root",
host:"localhost",
password:"",
database:"userdb",
});
app.use(express.json());
app.get("/users", (req, res) => {
db.query("SELECT * FROM users", (err, result) => {
res.json(result);
});
});
app.listen(3001, () => console.log('Listening on port 3001...'));
Could anyone please help me with this?

Unable to make api request to backend getting Axios network error

I was making one app with frontend react.js uses axios to api call and for backend i am using express and node with database mysql while making call to api url i am getting error for access denied error
Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''#'localhost' (using password: NO)
If this is not the case i will be getting axios network error
Axios Network error
i dont know which port to use if i use 3306 port it is already in use. Please take a look at code and suggest about optimization and a better way...
thank you....
config.js will be saving confiq for the connection db
config.js
const config = {
db: {
host: "localhost",
user: "new_user",
password: "password",
database: "db",
},
};
module.exports = config;
db.js will create the connection
db.js
const mysql = require("mysql");
const config = require("./config");
var con = mysql.createPool(config);
module.exports = con;
this is where the express and app server is supposed to create
index.js
const express = require("express");
var mysql = require("mysql");
const cors = require("cors");
const app = express();
app.use(express());
app.use(cors());
const TablesRoutes = require("./routes/TablesRoutes");
//routes
app.use("/", TablesRoutes);
app.listen(8000, () => {
console.log("Running on port :8000");
});
api routes to call from frontend
routes
const express = require("express");
const {
postTableDes,
alterTable,
showTables,
} = require("../Controllers/Tables");
const tablerouter = express.Router();
tablerouter.get("/", showTables);
tablerouter.post("/", postTableDes);
module.exports= tablerouter;
where showing the table act
controller
const con = require("../Config/db");
//get tables
const showTables = async (req, res) => {
//try {
var sql = `show tables`;
const data = await con.query(sql, (err, res)=>{
if(err){
console.log(err);
res.status(404).json({ error: "No show table" });
return;
}
console.log(res);
res.status(202).json(data);
});
module.exports = showTables;
Try to change the config.js to (the createPool call does not expect an object with db property):
const config = {
host: "localhost",
user: "new_user",
password: "password",
database: "db",
};
module.exports = config;

TypeError: req.getConnection is not a function

strong textI'm trying to create my first CRUD with Mysql, Node js and Express.js following a tutorial.
The problem is, in the video this code works, but in my visual give the error aboyt getConnection is not a function.
If anyones can help, I'll apreciate.
Server.js
const express = require("express");
const mysql = require("mysql");
const myconn = require("express-myconnection");
const routes = require("./routes");
const app = express();
const serverPort = process.env.PORT || 9000;
const dbOptions = {
host: "localhost",
user: "3306",
password: "123456",
database: "catalog",
};
//server running
app.listen(serverPort, () => {
console.log(`App listening at ${serverPort}`);
});
//routes
app.get("/", (req, res) => {
res.send("welcome to my API");
});
app.use("/phones", routes);
Routes.js
const express = require("express");
const routes = express.Router();
const myconn = require("express-myconnection");
routes.get("/", (req, res) => {
req.getConnection((err, conn) => {
if (err) return res.send(err);
conn.query("SELECT * FROM phones", (err, rows) => {
if (err) return res.send(err);
res.json(rows);
});
});
});
module.exports = routes;
UPDATE : solved.
IT WAS A POINT after catalog and I didn't write the port correctly!!!!!

Express taking forever to load mySQL query?

I'm trying to query a single line from a 28k record database as a test but it isn't going through but when I load up 'localhost:3001/api/get' it stays loading, even though my connection says success? Is it actually even connecting to the db?
my data bases schema is:
id | state_name | city
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
const mysql = require('mysql');
const cors = require('cors');
const db = mysql.createPool({
host: "localhost",
user: "root",
password: "password",
database: "states_city"
});
app.use(cors());
app.use(express.json());
app.use(bodyParser.urlencoded({extended:true}));
app.get('/api/get', (req, res)=>{
const sqlGet = "SELECT city FROM state_city city = 'Chicago'";
db.query(sqlGet, (err, res)=>{
console.log("success");
});
});
app.listen(3001, ()=>{
console.log("running on port 3001");
});
First you must make server running. Remove that API route you had set before running server.
app.listen(3001, ()=>{
console.log("running on port 3001");
});
Now you must create database connection. Create new file dbconn.js
var mysql = require('mysql');
const db = mysql.createPool({
host: "localhost",
user: "root",
password: "password",
database: "states_city"
});
Now create new connection:
var new_connection = mysql.createPool(
db
);
new_connection.on('connection', function (connection) {
console.log('DB Connection established');
connection.on('error', function (err) {
console.error(new Date(), 'MySQL error', err.code);
});
connection.on('close', function (err) {
console.error(new Date(), 'MySQL close', err);
});
});
// export connection
module.exports = new_connection;
Include that connection in other file:
var db_connection = require('../dbconn');
db_connection.query(query, params, function (error, results, fields) {
//Do your query
});
Read about project structure to make your code easy to edit.

How can I connect to mysql from nodejs?

I am working on a node JS project with mysql database. I have created a new file db.js that is used to connect to the database. My structure looks like below.
- server
- config
- db.js
- index.js
db.js
const mysql = require('mysql');
export default() =>{
const db = mysql.createConnection ({
host: 'localhost',
user: 'root',
password: '',
database: 'socha'
});
// connect to database
db.connect((err) => {
if (err) {
throw err;
}
console.log('Connected to database');
});
}
index.js
const express = require('express');
const dbConfig = require('./config/db');
const app = express();
My problem is I cannot connect to the db.js file from index.js file.
Can anybody tell me what mistake I have been doing.
Thank You
You can change your index.js and db.js to this.
This should create the connection if DB credentials are correct
db.js
const mysql = require('mysql');
const connect = () => {
const db = mysql.createConnection ({
host: 'localhost',
user: 'root',
password: '',
database: 'socha'
});
// connect to database
db.connect((err) => {
if (err) {
throw err;
}
console.log('Connected to database');
});
}
module.exports = {
connect
}
index.js
const express = require('express');
const dbConfig = require('./config/db');
const app = express();
// Connecting to db
dbConfig.connect();