简体   繁体   English

如何处理查询中的数组值?

[英]How to deal with array value in query?

I have a url which takes an array of topicName query like this 我有一个URL,需要这样的topicName查询数组

localhost:3000/api/?topicName=mobile&topicName=website 本地主机:3000 / API / topicName =移动&topicName =网站

and sometimes topicName only has one value like this 有时topicName只有一个这样的值

localhost:3000/api/?topicName=mobile

and below is how I use that query values in node JS 下面是我如何在节点JS中使用该查询值

let topicNameArray = [];
topicNameArray = req.query.topicName
let value = topicNameArray.map(function(e) {
return new RegExp(e, "i");
})

The problem I am facing is when the array query only has one value, my app will receive this message 我面临的问题是当数组查询只有一个值时,我的应用程序将收到此消息

"message": "topicNameArray.map is not a function

Can anyone suggest me a solution to deal with this problem? 谁能建议我解决这个问题的解决方案? Thank you so much! 非常感谢!

I believe you need to reform your querystring to mitigate the error you are experiencing. 我相信您需要改革您的查询字符串,以减轻您遇到的错误。

Currently I think you have a query string like such: localhost:3000/api/?topicName=mobile&topicName=website Here you have one query argument topicName and this is essentially a string. 当前,我认为您有一个查询字符串,例如: localhost:3000/api/?topicName=mobile&topicName=website在这里,您有一个查询参数topicName ,这实际上是一个字符串。 So when you have multiple instance of topicName key in your querystring you should have been replacing the the value at this key with the last instance of it in the querystring. 因此,当您在查询字符串中有topicName键的多个实例时,您应该已经将此键的值替换为查询字符串中它的最后一个实例。 So to send an array of values use the querystring structure as following: localhost:3000/api/?topicName[]=mobile&topicName[]=website (notice the square brace.) 因此,要发送值数组,请使用querystring结构,如下所示: localhost:3000/api/?topicName[]=mobile&topicName[]=website (注意方括号)。

Later you can check if the query argument is an array or not. 稍后,您可以检查查询参数是否为数组。 You can use Array.isArray function for that. 您可以使用Array.isArray函数。 Then you can perform your exception handling on later parts. 然后,您可以在以后的部分中执行异常处理。

you could check if topicName is an array using one of the following and convert it to an array like this 您可以使用以下方法之一检查topicName是否为数组,并将其转换为这样的数组

topicNameArray = req.query.topicName
if(req.query.topicName.constructor !== Array) {
   topicNameArray = [req.query.topicName];
}

Or like this, 还是这样

topicNameArray = req.query.topicName
if(!topicNameArray.length) { // only arrays have length property
   topicNameArray = [req.query.topicName];
}

Currently, topicNameArray is being reassigned to req.query.topicName which is actually a string when only one topicName is given. 当前, topicNameArray被重新分配给req.query.topicName ,当仅给出一个topicName时,它实际上是一个字符串。 You can check the type of req.query.topicName and if its a string turn it into an array like so: 您可以检查req.query.topicName的类型,以及是否将其字符串转换为数组,如下所示:

let topicNameArray = req.query.topicName;   // assume its an array
if (typeof topicNameArray === "string")  { // check if it's a string
  topicNameArray = [req.query.topicName];   // convert to array
}

or if you prefer the look of the ternary operator: 或者,如果您更喜欢三元运算符的外观:

let topicName = req.query.topicName;
let topicNameArray = (typeof topicName === "string") ? [topicName] : topicName;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM