0

I am trying to write an application with Node.js & PostgreSQL, currently I faced a problem in get by id, below is my code

app.get('/monsters/:id', (request, response, next) => {
  const { id } = req.params;
  pool.query('select * from monsters where id = $1', [id], (err, res) => {
    if (err) {
      return next(err);
    }else {
      response.json(res.rows);
    }
  });
});

it supposed to get the id I typed and return the value I stored in database with a table name called monster, however it just kept returning a blank object {}, I found the problem may because of the $1 part since my atom seemed not able to recognize $, what can I do to fix this problem or is there other way to write this instruction? Thank you!

starsneverfall
  • 137
  • 1
  • 2
  • 15
  • If you replace the `$1` with your actual ID, does it work? – Matt Fletcher Dec 17 '17 at 16:05
  • 1
    Since you are working with a SQL database for Node.js, is there a reason you are not using ORMs like Sequelize.js? Unless you are trying to achieve some crazy queries, I think it would be a lot simpler for your understanding and code. Otherwise, try wrapping the `$1` in a string in case it is not comparing correctly like `'select * from monsters where id = "$1"'` – hopelessmuffins Dec 17 '17 at 16:50

0 Answers0