DEV Community

Discussion on: Simple React JS and MySQL Integration -- CRUD App (Backend)

Collapse
 
byterbit profile image
Byter-Bit

I get a "net::ERR_CONNECTION_REFUSED" when trying to display the posts.

I've validated the database connectivity from Node, and thrown in more error reporting, but I'm still stuck.

Let me first thank you for all your good work, and say I'm sorry to post here with this problem. I'm a beginner in React and have learned a great deal from your good work.
So, first of all: I'm sure I can hit the local MYSQL database from within Node, using this test.js file called from the Node command line:

const express = require('express');
const mysql = require('mysql');
const db = mysql.createConnection({
host: "localhost",
user: "xxxx",
password: "xxxx",
database:"blog_posts"
})
db.connect((err) => {
if(err) {
console.log(err);
throw err;
}
console.log('it worked to MYSQL');
console.log(err);
});

These are of course the credentials I use in your db.js file.

I've found where the error is thrown in the MainPage.js file and I've put in more error trapping:

useEffect(()=>{
Axios.get("http://localhost:3002/api/get").then((data)=>{
console.log(data)
setPostList(data.data)
})
.catch(function (error) {
// handle error
console.log("problem here", error);
})
;
},[])

The result in the console is:

problem here Error: Network Error
at createError (createError.js:16)
at XMLHttpRequest.handleError (xhr.js:99)

I can't get beyond this. I don't think it's a CORS issue, as everything is on this machine.