93

I'm trying to convert req.params to Number because that is what I defined in my schema for year param.

I have tried

req.params.year = parseInt( req.params.year, 10 );  

and

Number( req.params.year);

and

1*req.params.year;

but non of them works. Do I need to install something?

7
  • Im sorry i didnt see the rest of the code you posted. Then try parseInt(req.params.year.replace(/[^0-9]/g, ''),10) Commented May 17, 2016 at 8:57
  • Possible duplicate of Problems with javascript "parseInt()" Commented May 17, 2016 at 9:00
  • can you console req.params.year and let me know what you get. Commented May 17, 2016 at 9:01
  • req. prams .year or req. params .year? Commented May 17, 2016 at 9:02
  • yep that was a typing mistake Commented May 17, 2016 at 9:05

4 Answers 4

167

You do not have to install something.

parseInt(req.params.year, 10);

should work properly.

console.log(typeof parseInt(req.params.year)); // returns 'number'

What is your output, if you use parseInt? is it still a string?

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

5 Comments

I think you are using express right? i never used this package, but could it be, the params are just read-only? what, if you create a own variable? var p = parseInt(req.params.year);
What is the different? Why does it work in this way? Sorry I'm learning nodejs so I'm curious.
@Sang Đặng There is not difference. The variable req.params is created by the express-package and it is set read-only. So you have to declare an own read-write variable.
Ah, that's why he can't put it back in the first try (in his question above.) Thank you for helping me.
I get ReferenceError: parseint is not defined with ` const { a } = req.body; console.log(parseint(a))` in app.post. parseInt is written like this, my fault.
15

Using parseInt() is a bad idea mainly because it never fails. Also because some results can be unexpected, like in the case of INFINITY.
Below is the function for handling unexpected behaviour.

function cleanInt(x) {
    x = Number(x);
    return x >= 0 ? Math.floor(x) : Math.ceil(x);
}

See results of below test cases.

console.log("CleanInt: ", cleanInt('xyz'), " ParseInt: ", parseInt('xyz'));
console.log("CleanInt: ", cleanInt('123abc'), " ParseInt: ", parseInt('123abc'));
console.log("CleanInt: ", cleanInt('234'), " ParseInt: ", parseInt('234'));
console.log("CleanInt: ", cleanInt('-679'), " ParseInt: ", parseInt('-679'));
console.log("CleanInt: ", cleanInt('897.0998'), " ParseInt: ", parseInt('897.0998'));
console.log("CleanInt: ", cleanInt('Infinity'), " ParseInt: ", parseInt('Infinity'));

result:

CleanInt:  NaN  ParseInt:  NaN
CleanInt:  NaN  ParseInt:  123
CleanInt:  234  ParseInt:  234
CleanInt:  -679  ParseInt:  -679
CleanInt:  897  ParseInt:  897
CleanInt:  Infinity  ParseInt:  NaN

3 Comments

this is not quite correct. parseInt('123abc')) throws an error, it doesn't validate as number 123
I am not sure about your envoroment. I have given output based on node version v10.2.1.
On node 13.8 parseInt('123abc') -> 123
5

You can parse string to number in several ways.

  • Number()
  • parseInt()
  • parseFloat()

Example:

Number("256.123") //returns 256.123

Number("252px") //returns NaN

parseInt("256.123") //returns 256

parseInt("256px") //returns 256

parseFloat("256") //returns 256

parseFloat("256.123") //returns 256.123

parseFloat("256.23px") //returns 256.23

Comments

3

Not a full answer Ok so this is just to supplement the information about parseInt, which is still very valid. Express doesn't allow the req or res objects to be modified at all (immutable). So if you want to modify/use this data effectively, you must copy it to another variable (var year = req.params.year).

Comments

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.