Trying to figure out on how to fetch data from mysql and display it in ReactJS. I'm using NodeJS on the backend along with express. I tried a code snippet found on the internet but it doesn't work as it is expected.
Here's what i get when i run the react app.
TypeError: http.ServerResponse is undefined
My NodeJS code
//require mysql, http and express
//const connection = createConnection({with host, user, pass, db});
const app = express();
app.get('/posts', function(request, result){
connection.connect();
connection.query("SELECT * FROM 'some_table';", function(err, results, fields){
if(err) throw err;
result.send(results);
})
connection.end();
})
app.listen(3000);
My React code
class Display extends React.Component{
constructor(props){
super(props);
this.state={ posts : [] };
fetch('http://localhost:3000/posts/')
.then(response =>{
response.json();
})
.then(posts => {
this.setState({posts})
})
.then( (err) => {
console.log(err);
})
}
render(){
return(
<div>
<ul>
{this.state.posts.map( post =>
<p>
<li>Some Text_1: {post.db_col_1}</li>
<li>Some Text_2: {post.db_col_2}</li>
<li>Some Text_3: {post.db_col_3}</li>
</p>
)}
</ul>
</div>
)
}
}
export default Display;