[英]Getting `undefined` on res.query for a JSON query string using qs library
I'm using qs
to build JSON based query parameters to my REST API call:我正在使用qs
为我的 REST API 调用构建基于 JSON 的查询参数:
On client side:在客户端:
import qs from "qs";
let query = {
dateTime: { $gte: 1664557995000 }
};
let q = qs.stringify(query);
let url = "http://localhost:3000/testapi/events?" + q;
console.log(url) <<=== http://localhost:3000/testapi/events?dateTime%5B%24gte%5D=1664557995000
let response = await fetch(url, {
method: "GET",
headers: new Headers({
Accept: "application/json",
"Content-Type": "application/json",
}),
On the server side I'm getting undefined on res.query
:在服务器端,我在res.query
上未定义:
app.get(
"/testapi/events",
async (req, res) => {
console.log(res.query) <<=== undefined
let query = qs.parse(res.query);
console.log(query) <=== undefined
}
)
I'm getting undefined
for res.query and therefore I cannot parse it.我对 res.query undefined
,因此我无法解析它。
At the end, no parameters are being sent to the server.最后,没有参数被发送到服务器。
I have no clue why is that happening, as the query string contains the parameters ( dateTime%5B%24gte%5D=1664557995000
).我不知道为什么会这样,因为查询字符串包含参数( dateTime%5B%24gte%5D=1664557995000
)。
I need to rebuild the JSON on the server side to proceed to a mongo query.我需要在服务器端重建 JSON 以继续进行 mongo 查询。 JSON types will involve even more complex parameters ($or, $and, etc). JSON 类型将涉及更复杂的参数($or、$and 等)。
res
is the response object. You receive the query on the req
object - ie, you should be using req.query
, not res.query
. res
是响应 object。您收到关于请求req
的查询 - 即,您应该使用req.query
,而不是res.query
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.