I want to connect my database to my react app however I'm very confused on how to do this. I made the database using SQL in SQL Server Management Studio. I've tried using express to connect it to my app however I know there's a lot missing from my code. What do I need to add/change to my code to connect it to my project?
UPDATE:
I made an express app and wrote some code in users.js:
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
router.get('/', function(req, res, next) {
connection.query('SELECT * from project_ideas', function (error, results, fields) {
if (error) throw error;
res.send(JSON.stringify(results));
});
});
module.exports = router;
I added this code to App.js in the React app:
var React = require('react')
var App = React.createClass({
getInitialState: function() {
return {
members: []
};
},
componentDidMount() {
fetch('/users')
.then(res => res.json())
.then(project_ideas => this.setState({ project_ideas: project_ideas }));
},
render: function() {
return (
<div className="Ideas">
<h1>Ideas</h1>
{this.state.project_ideas.map(project_ideas =>
<div key={project_ideas.id}>{project_ideas.idea}</div>
)}
</div>
);
}
});
module.exports = App;
