javascript 如何检查 node.js 中的查询字符串中是否存在参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15834336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to check if a parameter is present in the querystring in node.js
提问by Raj
How to validate if a parameter is exits in the querystring or not in the node.js ?
如何验证参数是否存在于查询字符串中或不存在于 node.js 中?
I am validating like this
我像这样验证
if(prm1 == null)
return error
But actually in the console.log(prm1) says undefined..
但实际上在 console.log(prm1) 中说 undefined ..
How to validate this exactly ?
如何准确验证这一点?
回答by throrin19
If the parameter is not present in your queryString it is returned as undefined
. The parameter wouldn't return null
because it wasn't initialised.
如果您的 queryString 中不存在该参数,则它将作为undefined
. 参数不会返回,null
因为它没有初始化。
try this :
试试这个 :
if(typeof prm1 != 'undefined')
return "error";