Proto
Proto

Reputation: 774

Why is req.params returning undefined?

I've checked two similar questions here and neither of the things suggested in the comments are working for me.

app.get('/:id', function(req,res) {
  console.log(req.params.id);
});

app.get('/:id', function(req, res) {
  db.query("SELECT * FROM entries WHERE id = $1", [req.params.id], function(err, dbRes) {
    if (!err) {
      res.render('show', { entry: dbRes.rows[0] });
    }
  });
});

As you can see, I've tried logging the result to the console to see what's going on. Visiting the URL in question just makes the page load until it times out. In the console, I get "undefined".

How do I define req.params? Or where is it's definition being pulled and why isn't it returning the values?

Full context: http://pastebin.com/DhWrPvjP

Upvotes: 1

Views: 10743

Answers (4)

DC IT
DC IT

Reputation: 221

I know I'm late to the party but this post helped me debug my issue so I figured I'll add my suggestion in hopes it will help someone else.

If you are using mysql2 with promises

const mysql = require("mysql2");

const pool = mysql.createPool({
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_DATABASE
});

module.exports = pool.promise();

Then you need to use promises in your request.

router.get("/:id", (req, res) => {
  mysql
    .execute("SELECT * FROM entries WHERE id = $1", [req.params.id])
    .then(result => {
      res.send(result[0]);
    })
    .catch(err => {
      console.log(err);
    });
});

I spent hours debugging my code only to realize I was using promise() in my connection. Hope this helps as this post helped me debug.

Upvotes: 0

Don
Don

Reputation: 526

Just tested your code and it works fine. I think you might be missing your url parameter. It should be http://localhost:3000/1 - or whatever ID you're trying to retrieve. Try it out.

Also, you should pass the extended option to your bodyParser.urlencode method: express throws error as `body-parser deprecated undefined extended`

Edit: To specifically answer your question about defining request parameters. You don't have to do anything to define request parameters other than make sure that you're passing in the correct URL. Express takes care of parsing the URL and defining the request parameters for you. So, if you go to the URL http://localhost/jimbob on your server then the value passed in for the id parameter will be available as req.params.id. See this link on request parameters for more info.

Edit 2: You could try debugging your app to see what you get. Here's a link on how to enable debugging in Express and how to use node-inspector for debugging. I saw that your running this on Ubuntu. So, there may be something weird there that I'm not aware of. (I'm running it on a Mac.)

I would also check the version of Node that you're running on the computer(s) that the app works on and check the version of Node on your Ubuntu environment (or whatever computers the app doesn't work on).

Upvotes: 1

KibGzr
KibGzr

Reputation: 2093

try this req.param('id') :D. It may be working for you

Upvotes: 0

Snymax
Snymax

Reputation: 357

app.get('/:id', function(req, res) {
  db.query("SELECT * FROM entries WHERE id = $1", [req.params.id], function(err, dbRes) {
    if (!err) {
      res.render('show', { entry: dbRes.rows[0] });
    }
  });
});

in your code the url would be localhost/some-id req.params.id would equal some-id, params are pulls straight from the url string, if you are trying to send info with post or get methods you want to use req.body and req.query respectively. I dont see any reason you wouldnt be able to get the id unless the url is wrong

or if you need to do it manually

app.get('/:id', function(req, res) {
  //if no req.params and assuming the id is the last item in the url
  var urlArray = req.url.split('/'),
      id = urlArray[urlArray.length-1];
  db.query("SELECT * FROM entries WHERE id = $1", [req.params.id], function(err, dbRes) {
    if (!err) {
      res.render('show', { entry: dbRes.rows[0] });
    }
  });
});

Upvotes: 0

Related Questions