Search code examples
node.jsstringtype-conversion

convert string to number node.js


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?


Solution

  • 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?