首页 > 解决方案 > req.params.id 未定义,ReferencesError : id 未定义

问题描述

这是浏览器中的网址

http://localhost:8888/update/1

这是路线的代码

router.addRoute("/update/:id", (req, res) => {
  conn.query(
    "select * from mahasiswa where ? ",
    { no_induk: req.params.id },
    function (err, rows, field) {
      if (rows.length) {
        if (req.method.toUpperCase() == "POST") {
        } else {
          let html = view.compileFile("./tamplate/form_update.html")();
          res.writeHead(200, { "Contact-Type": "text/html" });
          res.end(html);
        }
      } else {
        let html = view.compileFile("./tamplate/form_update.html")();
        res.writeHead(200, { "Contact-Type": "text/html" });
        res.end(html);
      }
    }
  );


});

请帮帮我我不知道

标签: node.js

解决方案


我认为最好使用 router.post

例如:

router.post("/update/:id", (req, res) => {
  conn.query(
    "select * from mahasiswa where ? ",
    { no_induk: req.params.id },
    function (err, rows, field) {
      if (rows.length) {
          let html = view.compileFile("./tamplate/form_update.html")();
          res.writeHead(200, { "Contact-Type": "text/html" });
          res.end(html);
        }
      }
});

推荐阅读