0

Given the URLs:

  1. http://somedomain.com/?s=something

  2. http://somedomain.com/?s=

  3. http://somedomain.com/

Passing through the code:

var queryData = url.parse(request.url, true).query;
if (queryData["s"]) {
    console.log("s present in hash");
} else {
    console.log("s NOT present in hash");
}

I would expect URLs 1 and 2 to test true. They do in other langs/libs I have used before. In querystring parsing code they do not treat #2 as { "s" : null } it just doesn't put s in the hash.
Is there another node package I should use if I want this behavior or another way I should be able to test for s var in the querystring?

1
  • how about if (queryData["s"] && queryData["s"].length > 0) { Commented Apr 30, 2014 at 15:00

1 Answer 1

1

Your variable s is in the hash. It's equal to an empty string, which is exactly what you have in your URL. When coerced into a boolean, it will evaluate as false.

> url.parse('http://somedomain.com?s=', true).query
{ s: '' }

> !!url.parse('http://somedomain.com?s=', true).query.s
false
Sign up to request clarification or add additional context in comments.

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.