I want to get the the data for specified user from MySQL, there isn't any error, but nothing display This is because in the index.js in the server, the id undefined, how can i take the id from the front end! What is the error in my code?
This is in the front end (React js)
function EditCourse() {
const [CourseID, setCourseID] = useState(0);
const [Name, setName] = useState("");
const [Description, setDescription] = useState("");
const [TeacherSSN, setTeacherSSN] = useState(0);
const [data, setData] = useState([]);
const { id } = useParams();
useEffect(() => {
debugger;
Axios.get(`http://localhost:3003/fetchdata/${id}`, {
CourseID: CourseID,
Name: Name,
Description: Description,
TeacherSSN: TeacherSSN,
}).then(result => setData(result.data));
console.log("success")
debugger;
}, [id]);
return(
<div>
{data.map(item => {
return (
<div key={item.Id}>
<h2>
رقم الدورة :
</h2>
<br />
<input type="text" value={item.CourseID}/>
<h2>
اسم الدورة :
</h2>
<br />
<input type="text" value={item.Name}/>
<br />
<h2>
رقم هوية مدرب الدورة :
</h2>
<br />
<input type="text" value={item.TeacherSSN}/>
<br />
<p> تفاصيل الدورة :</p>
<br />
<TextField
label={item.Description}
color="secondary"
variant="outlined"
multiline/>
</div>
) }) }
</div>
) }
And this is the index.js in the server
app.get(`/fetchdata/:id`,(req, res) => {
const id = req.body.id;
console.log(id);
db.query(`SELECT CourseID, TeacherSSN, Name, Description FROM course WHERE CourseID=${id}`, (err, result) => {
if (err) {
console.log(err)
} else {
res.send(result)
}
})
});
CodePudding user response:
You are passing the id
through URL params, while you are trying to get the id
from the request body.
Replace
const id = req.body.id;
To
const id = req.params.id;