i have app built on node.js backend and react.js frontend. For code in react keeps throwing an error:
import './App.css';
import React, {useState, useEffect } from 'react';
import Axios from 'axios';
function App() {
const [material, setMaterial] = useState("");
const [materialList, setMaterialList] = useState([])
useEffect(()=> {
Axios.get('http://127.0.0.1:3001/api/get').then((response)=>{
setMaterialList(response.data);
});
}, []);
return (
<div className="App">
<h1>TEST</h1>
<div className="form">
<label>Search Data from DB</label>
<input type="text" name="material" />
{materialList.map((val)=>{
return
<h1>
Material: {val.material}
</h1>
})
}
</div>
</div>
);
}
export default App;
It still reports me an error that materialList.map is not function.
And I can't always figure out why.
EDIT: Here is api section from backend part index.js:
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const sql = require("mssql");
const dbConfig = {
server: 'host',
user: 'user',
password: 'pass',
database: 'database',
options: {
trustedConnection: true,
encrypt: true,
enableArithAbort: true,
trustServerCertificate: true,
},
};
app.use(cors());
app.use(express.json());
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.get('/api/get', (req,res)=>{
var dbConn = new sql.ConnectionPool(dbConfig);
dbConn.connect().then(function () {
var request = new sql.Request(dbConn);
request.query("Select TOP(10) * FROM [dbo].[Visual]").then(function (resp) {
res.send(resp);
dbConn.close();
}).catch(function (err) {
console.log(err);
dbConn.close();
});
}).catch(function (err) {
console.log(err);
});
});
app.get('/', (req,res) => {
res.send("Hello Friend");
});
app.listen(3001,()=>{
console.log("running on port 3001");
});
When I run the node.js server itself and give url / api / get it will show me correctly. But it is true that in recordset format. Maybe in this error?