4

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

9
  • 1
    Did you define your /topic/create route before something like /topic/:id? Commented Jan 10, 2020 at 5:56
  • yes i made /topic/:id Commented Jan 10, 2020 at 5:58
  • @tadman i add /topic/:id code in my question Commented Jan 10, 2020 at 6:01
  • whats $1, have you defined it purposely Commented Jan 10, 2020 at 6:11
  • 1
    @ArunK i push that id in html ~~~ each topic in topics li a(href='/topic/'+topic.id)= topic.title ~~~ like this Commented Jan 10, 2020 at 6:14

2 Answers 2

4
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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was perfectly working. thanks a lot. I didn't know express was important about function order
3

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.

2 Comments

Thank u for yout help. I didn't know function order was important in express
Glad to be of help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.