【问题标题】:How to get data for specified user from front end(React js) to back end(Node js, MySQL)如何从前端(React js)到后端(Node js,MySQL)获取指定用户的数据
【发布时间】:2022-01-04 07:42:30
【问题描述】:

我想从 MySQL 中获取指定用户的数据,没有任何错误,但没有任何显示 这是因为在服务器的 index.js 中,id 未定义,我怎么能从前端获取 id! 我的代码有什么错误?

这是在前端(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>
) }

这是服务器中的 index.js

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)
    }
  })
});

【问题讨论】:

    标签: mysql node.js reactjs


    【解决方案1】:

    您正在通过 URL 参数传递 id,而您正试图从请求正文中获取 id

    替换

    const id = req.body.id;
    

    const id = req.params.id;
    

    【讨论】:

    • 我在更改时添加到所有文本,但我无法更改文本中的值,我不会写。
    • 我如何在文本中写入并更改值?
    猜你喜欢
    • 2021-11-28
    • 2020-07-09
    • 2021-07-01
    • 2020-05-17
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多