1

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

Proto
  • 734
  • 6
  • 21
  • 2
    can you copy and past the url that you are trying to get id from and check your console for a get request matching the url... req.params is built on a string so if you want to get the id of url '/users/1235235632' it would be app.get('/users/:id' :id is a placeholder for the string u put after the slash – Snymax Dec 06 '14 at 23:56
  • 1
    You are not handling the query error, so the connection will set there until it times out. You should at least be calling the error handler. – loganfsmyth Dec 07 '14 at 02:37

4 Answers4

1

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

Community
  • 1
  • 1
Don
  • 512
  • 2
  • 7
  • 22
  • If you tested my coded and said it works fine, then how do you suppose I'm not including the URL parameters since I'm passing it the same thing as you? That doesn't make sense. Thanks for testing it, but the question isn't whether it works for you, but why it's not working for me. – Proto Dec 07 '14 at 04:05
  • I understand that. That's why I posted a sample URL. That should fix your problem. – Don Dec 07 '14 at 04:06
  • That returns undefined. I don't actually see why that should work anyway. Perhaps you can explain why that should work? Can you take a stab at the questions in the OP while you're at it? This kind of information can help me troubleshoot for myself. Thanks. – Proto Dec 07 '14 at 20:32
  • I updated my answer. Please let me know if you need further clarification. – Don Dec 07 '14 at 21:24
  • Thanks for all your help. You've been great. Unfortunately, the issue persists, but I've had enough developers test my code on their own machines and find that the code works for them. Something's up with my particular installation that I can't really figure out. I don't see how ...but that seems to be the only thing that could be wrong. I'll tag this as a solution, because it's definitely working for most people. Just not for me. Thanks again. – Proto Dec 08 '14 at 01:56
  • You're welcome. I'd hate to see you not be able to solve this. I've updated my answer again to include some other things that you can try to solve this. Please let me know if you're able to figure it out or if you need more help. – Don Dec 08 '14 at 14:31
  • Hi , Suppose "/mobile/custom/******/deviceVersion/:deviceType" is my request . could you please tell me how to check "deviceType" against undefined? – Arj 1411 Jan 27 '17 at 07:23
0
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] });
    }
  });
});
Snymax
  • 327
  • 4
  • 18
  • The thing is: this exact code works if run on a Mac. It's difficult to understand how changing platforms (Ubuntu) suddenly produces an "undefined" error. There's something about how req.params and :id are called that Ubuntu apparently does very differently than a mac. So that's why part of my question is how does one define req.params? – Proto Dec 07 '14 at 00:33
  • well i suppose you could use req.url if you have access to that I'll modify my answer – Snymax Dec 07 '14 at 00:52
0

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

KibGzr
  • 1,773
  • 8
  • 11
  • No, that actually causes another, worse error as it should ('id' isn't anything on it's own). But thanks for trying :) – Proto Dec 07 '14 at 20:33
0

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.

DC IT
  • 221
  • 2
  • 5