I am very new to web design (literally no prior knowledge) and is currently trying to create a website which pulls data from my MySQL database using Nodejs and display it on my HTML page with Reactjs. Any help would be highly appreciated
I previously copied the code from how to display json data with ReactJs in the table, but when I used my URL from my localhost server with the JSON data in it it just shows me the error "TypeError: this.state.data.map is not a function".
this is my app.js
class App extends React.Component {
constructor(){
super()
this.state = {
data: []
}
}
componentDidMount() {
$.ajax({
url: "http://localhost:4000/stockin",
type: "GET",
dataType: 'json',
ContentType: 'application/json',
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(jqXHR) {
console.log(jqXHR);
}.bind(this)
})
}
render() {
return (
<table>
<tbody>{this.state.data.map(function(item, key) {
return (
<tr key = {key}>
<td>{item.No}</td>
<td>{item.Stocktype}</td>
<td>{item.Binid}</td>
<td>{item.Stockid}</td>
</tr>
)
})}</tbody>
</table>
)
}
}
ReactDOM.render(<App/>, document.getElementById('app'))
And this is my json data
{"stockin":[{"No":1,"Stocktype":"XM21","Binid":"1234124","Stockid":"135215215","Datestockin":"2019-05-05T16:00:00.000Z","Quantity":52,"Remarks":"Good stock"},
{"No":2,"Stocktype":"XM22","Binid":"2352342","Stockid":"123456721","Datestockin":"2019-05-05T16:00:00.000Z","Quantity":21,"Remarks":"Good stock"},
{"No":3,"Stocktype":"screw","Binid":"2432342","Stockid":"123456721","Datestockin":"2019-05-05T16:00:00.000Z","Quantity":23,"Remarks":"Good stock"},
{"No":4,"Stocktype":"screw","Binid":"1325123","Stockid":"242353223","Datestockin":"2019-04-30T16:00:00.000Z","Quantity":32,"Remarks":"Screws for bins"}]}
I wish to be able to display the data in a table form on my HTML website in the end.