尝试更新我的 Node.js 应用程序中的用户,但出现错误。但是,当我使用 Postman 时,更新可以正常工作

问题描述 投票:0回答:1
// Controller
exports.updatedTraningUserData = (req, res) => {
    const traningId = req.params.id;
    const updatedUserData = req.body; // The updated user data from the request body

    // Update the user in the database
    const sql = 'UPDATE traning SET certificationCode = ?, fullName = ?, company = ?, position = ?, email = ?, telephone = ?, date = ?, title = ?, futureTopics = ? WHERE id = ?';
    const values = [updatedUserData.certificationCode, updatedUserData.fullName, updatedUserData.company, updatedUserData.position, updatedUserData.email, updatedUserData.telephone, updatedUserData.date, updatedUserData.title, updatedUserData.futureTopics, traningId];

    db.query(sql, values, (err, results) => {
      if (err) {
        console.error('Error updating the user: ' + err.message);
        res.status(500).send('Error updating the user');
      } else {
        const traningUpdate = results;
        console.log(traningUpdate);
        console.log('User updated successfully');
        res.status(200).send('User updated successfully');
      }
    });
};

内部

console.log(traningUpdate)

enter image description here

但是当我使用相同路径的邮递员时:http://localhost:3000/traning-user/12
使用 json 传递值它可以工作!

enter image description here

这里是我的 traningUser.ejs 我的表单在哪里:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>traning User</title>
</head>
<body>
    <h1><%= traning.fullName %></h1>
    <div class="container">
      <form class="is-readonly" action="/traning-user/:id" method="post">
        <div class="form-group">
          <label for="certificationCode">Certification Code</label>
          <input type="text" class="form-control is-disabled" id="certificationCode" placeholder="<%- traning.certificationCode %>" value="<%- traning.certificationCode %>" name="certificationCode" disabled>
        </div>
      <div class="form-group">
          <label for="fullName">Fullname</label>
          <input type="text" class="form-control is-disabled" id="fullName" placeholder="fullName" value="<%- traning.fullName %>" name="fullName" disabled>
        </div>
        <div class="form-group">
          <label for="company">Company</label>
          <input type="text" class="form-control is-disabled" id="company" placeholder="<%- traning.company %>" value="<%- traning.company %>" name="company" disabled>
        </div>
*****ECT...******
</body>
</html>

我的route.js :

// Fichier traningRoutes.js dans le répertoire routes
const express = require('express');
const router = express.Router();
const traningController = require('../controllers/traning');

router.post('/traning', traningController.createTraningUser);
router.delete('/traning/:id', traningController.deleteTraningUsers);
router.get('/traning-user', traningController.getTraningUsers);
router.post('/traning-user/:id', traningController.updatedTraningUserData); //HERE
router.get('/display/:id', traningController.test);
router.get('/traning-user/:id', traningController.getTraningUserById);

module.exports = router;

我想通过 HMTL 表单使用发布请求来更新我的数据库

html node.js express ejs
1个回答
0
投票

该错误可能是由于此处的行引起的:

...
    <div class="container">
      <form class="is-readonly" action="/traning-user/:id" method="post"> <--this line
        <div class="form-group">
...

在操作字段中,您没有正确绑定 id。我相信ejs,你需要像这样绑定变量:

      <form class="is-readonly" action="/traning-user/<%= training.id %>" method="post">
© www.soinside.com 2019 - 2024. All rights reserved.