0

I have an api that calls:

http://example.com/games?maxplayers=10

then I use

url.parse(req.url,true).query

which returns:

{ maxplayers: '10' }

I would like to replace the string that contains 10 with the actual int value of 10 so I can query my database and return a result. As of right now it returns no result because it recognizes the 10 as a string. Any insight?

EDIT:

I tried this:

var example = parseInt(url.parse(req.url,true).query)
  console.log(example);

However all it prints is "NAN"

I need it to return:

{ maxplayers: 10 }
user2510809
  • 235
  • 3
  • 14
  • http://stackoverflow.com/questions/1133770/how-do-i-convert-a-string-into-an-integer-in-javascript – Valentin Waeselynck Feb 20 '15 at 17:53
  • This looks like JSON. You should use a JSON parser for that. – ByteHamster Feb 20 '15 at 17:53
  • 2
    @ByteHamster: It does not look like JSON at all.`url.parse(req.url,true).query` returns an **object**. What you see is the literal representation of that object. If it was JSON, the return value would be `"{\"maxplayers\":\"10\"}"` (i.e. a string containing JSON). – Felix Kling Feb 20 '15 at 17:54
  • @FelixKling Oh, you are right, sorry – ByteHamster Feb 20 '15 at 17:59
  • 1
    Well `.query` is an **object**. You want to convert the value of `.query.maxplayers`. Or iterate over the properties of that object and converted them. Learn more about objects from the MDN JavaScript tutorial: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects – Felix Kling Feb 20 '15 at 18:38

0 Answers0