1

I'm using qs to build JSON based query parameters to my REST API call:

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:

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.

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).

I need to rebuild the JSON on the server side to proceed to a mongo query. JSON types will involve even more complex parameters ($or, $and, etc).

1 Answer 1

2

res is the response object. You receive the query on the req object - i.e., you should be using req.query, not res.query.

Sign up to request clarification or add additional context in comments.

1 Comment

This was idiot from myself... A typo mistake after some hours programming... Thanks for bringing me back...

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.