0

I have code in a Node.JS app, only partly working.

Though I think I have figured out in which case there are problems, I do not know the proper solution to fix it.

Here is the relevant code for the question:

  app.get('/Path', (req, res) => {
    .....
    let kickVal = req.url.substring(9,14) // Extracting a 5 digits string from the URL.
    // Instead of the line above, I have also tried to use querystring to get the value for kickVal, but it did not work any better.
    .....
    doSomeUsefulStuff(res,kickVal)
  })
  
  function doSomeUsefulStuff(response,kickStr=null) {
    ... do some work using kickStr ...
    // Here kickStr does not always shows up as expected.
  }

For example when using:

http://localhost:3000/Path?fp=10234

http://localhost:3000/Path?fp=31204

then all is fine I get the values, 10234 and 31204 for kickStr inside doSomeUsefulStuff as one would expect.

But when using:

http://localhost:3000/Path?fp=01243

http://localhost:3000/Path?fp=04231

I get 675 in the first case and 2201 in the second, when I expected the strings '01243' and '04231'.

Generally speaking when the string of digits contains a leading zero, things go wrong. I suspect that is because req.url is not a simple string as I would like it to be. But what is the correct way to solve this issue?

It should be noted that I do not do any kind of conversion to or from Int. The code above (kickVal = req.url.substr...) is the only thing I do to extract the value (kickVal). Why is this not working is certainly my first question.

Michel
  • 10,303
  • 17
  • 82
  • 179
  • 2
    `req.query.fp`. see: [How to get GET (query string) variables in Express.js on Node.js?](https://stackoverflow.com/questions/6912584/how-to-get-get-query-string-variables-in-express-js-on-node-js) – traynor Feb 26 '23 at 11:08
  • @traynor. I am trying to look into the answer you suggest. But I can already say that using this code "kickVal = req.query.fp" to set the value gives me exactly the same result as before. It doesn't solve anything. – Michel Feb 26 '23 at 11:37
  • @traynor. I have also tried to set the value, following what is the accepted answer but it gives me exactly the same result as before, without solving anything. – Michel Feb 26 '23 at 11:53
  • try logging the value right away, add: `console.log(req.query.fp)`, it should read `{fp: '01234'}`.. or is there something else? if it's reading '01234', then there's something else setting those values to the variable, which I'd say might be the real problem, instead of express not parsing query string correctly, because I can't reproduce.. – traynor Feb 26 '23 at 12:34
  • For some reason I can't see any console.log result. But I can see what doSomeUsefulStuff receives, in a div that I have set for that. And it is what I wrote in my post. – Michel Feb 26 '23 at 12:52
  • I may be making a basic mistake. But I have a hard time to see why this issue happens only with leading zeros, if I am dealing with strings. – Michel Feb 26 '23 at 13:18
  • @traynor. You write "I can't reproduce". Can I ask what require you are using so that it works for you. I may be missing something on that side ... – Michel Feb 26 '23 at 13:34
  • I just log what I suggested. just add it below, like this: `app.get('/Path', (req, res) => { console.log(req.query);`, before you process it – traynor Feb 26 '23 at 14:57

1 Answers1

0

I finally found out what was making things go wrong. I needed to put quotes around kickVal.

In other words, instead of this line of code:

doSomeUsefulStuff(res,kickVal)

I had to use this one:

doSomeUsefulStuff(res,"'"+kickVal+"'")

And all was solved.

Michel
  • 10,303
  • 17
  • 82
  • 179