简体   繁体   中英

Nodejs invalid input syntax for integer error

I using postgres sql, nodejs, express

     app.get("/topic/create", function(req, res) {
      var sql = "SELECT id, title FROM topic";
      client.query(sql, function(err, res2) {
        console.log(res2.rows);
        //res.render("create", { topics: res2.rows });
      });
    });

this code is my router code but when i enter that url it was error

 error: invalid input syntax for integer: "create" name: 'error', length: 110, severity: 'ERROR', code: '22P02', line: '62',routine: 'pg_atoi'

i dont know reason because sql was working on other url only that app.get code doesn't working

    //라우팅 작업
    app.get("/", function(req, res) {
      res.redirect("/topic");
    });
    app.get("/topic", function(req, res) {
      var sql = "SELECT id, title FROM topic";
      client.query(sql, function(err, res2) {
        if (err) {
          console.log(err);
        } else {
          res.render("view", { topics: res2.rows });
        }
      });
    });
app.get("/topic/:id", function(req, res) {
  var id = req.params.id;
  var sql1 = "SELECT id, title FROM topic";
  var sql2 = "SELECT * FROM topic where id=$1";
  client.query(sql2, [id], function(err, res2) {
    if (err) {
      console.log(err);
    } else {
      client.query(sql1, function(err, res3) {
        if (err) {
          console.log(err);
          res.status(500).send("Internal Server Error");
        } else {
          var list = [];
          var result = res3.rows;
          for (var i = 0; i < result.length; i++) {
            list.push(res3.rows[i]);
          }
          res.render("view", { details: res2.rows, topics: list });
        }
      });
    }
  });
});

this is my router code it was same. this code is good working

i dont know why only that url make error

as @tadman mentioned, you need to define app.get("/topic/create function before the app.get("/topic/:id" function. otherwise, express thinks that you are executing app.get("/topic/:id" and the id is create .

Hope this helps.

app.get("/topic/create", function(req, res) {
      var sql = "SELECT id, title FROM topic";
      client.query(sql, function(err, res2) {
        console.log(res2.rows);
        //res.render("create", { topics: res2.rows });
      });
    });

You have to put this router first then below one,

app.get("/topic/:id", function(req, res) {
  var id = req.params.id;
  var sql1 = "SELECT id, title FROM topic";
  var sql2 = "SELECT * FROM topic where id=$1";
  client.query(sql2, [id], function(err, res2) {
    if (err) {
      console.log(err);
    } else {
      client.query(sql1, function(err, res3) {
        if (err) {
          console.log(err);
          res.status(500).send("Internal Server Error");
        } else {
          var list = [];
          var result = res3.rows;
          for (var i = 0; i < result.length; i++) {
            list.push(res3.rows[i]);
          }
          res.render("view", { details: res2.rows, topics: list });
        }
      });
    }
  });
});

this will work, Because express take and match with first one. It get failed, That's why you got this error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM