89

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?

user3488862
  • 1,329
  • 2
  • 12
  • 16

4 Answers4

158

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?

Tusk
  • 1,647
  • 1
  • 10
  • 8
  • 5
    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);` – Tusk May 17 '16 at 09:19
  • What is the different? Why does it work in this way? Sorry I'm learning nodejs so I'm curious. – Sang Dang May 17 '16 at 10:11
  • 4
    @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. – Tusk May 17 '16 at 13:41
  • Ah, that's why he can't put it back in the first try (in his question above.) Thank you for helping me. – Sang Dang May 17 '16 at 18:27
  • 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. – Timo Mar 04 '21 at 18:56
13

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
tejp124
  • 356
  • 3
  • 12
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).

Werlious
  • 583
  • 6
  • 15
3

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

Alchemist
  • 325
  • 1
  • 17