如何检查 node.js 中的查询字符串中是否存在参数

Raj*_*Raj 1 javascript node.js

如何验证参数是否存在于查询字符串中或不存在于 node.js 中?

我像这样验证

if(prm1 == null)
return error
Run Code Online (Sandbox Code Playgroud)

但实际上在 console.log(prm1) 中说 undefined ..

如何准确验证这一点?

thr*_*n19 5

如果您的 queryString 中不存在该参数,则它将作为undefined. 参数不会返回,null因为它没有初始化。

尝试这个 :

if(typeof prm1 != 'undefined')
    return "error";
Run Code Online (Sandbox Code Playgroud)

  • 最好的方法是 if(typeof prm1 != 'undefined') 因为 typeof 在字符串中返回类型。如果您尝试直接测试未定义类型,请使用 if(prm1 !== undefined) 但它很慢 (2认同)