Got a couple questions here. I've been using the express get method
for a search query and it's fetching the requested tickets without any issues. However, I keep encountering errors even though the method itself is functioning properly. So, my first question is: why am I consistently getting these errors and how can I go about resolving them?
Here's what the console is displaying:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:558:11)
at ServerResponse.header (C:Project\server\node_modules\express\lib\response.js:771:10)
at ServerResponse.json (C:Project\server\node_modules\express\lib\response.js:264:10)
at ServerResponse.send (C:Project\server\node_modules\express\lib\response.js:158:21)
at C:Project\server\index.ts:63:7
at Layer.handle [as handle_request] (C:Project\server\node_modules\express\lib\router\layer.js:95:5)
...
Here's the code snippet in question:
app.get(APIPath, (req, res) => {
//@ts-ignore
const search:String = req.query.search;
if(search !== undefined){
const filteredTickets = tempData.filter((t) => (t.title.toLowerCase() + t.content.toLowerCase()).includes(search.toLowerCase()));
res.send(filteredTickets);
}
// @ts-ignore
const page: number = req.query.page || 1;
const paginatedData = tempData.slice((page - 1) * dataProps.getPageSize(), page * dataProps.getPageSize());
console.log("Server: Page " + page + " was sent!");
console.log("Server: Search query: " + search)
res.send(paginatedData);
});
In this function, I'm attempting to handle two queries simultaneously. The page query functions flawlessly, but I wanted to combine both so that I could still paginate results after filtering by other parameters (with a page limit of 20). Unfortunately, they're not playing nice together as I'm unsure of the proper approach. This leads me to the second question: How do I effectively combine both queries to resemble something like
https://localhost:3000/?page=[Page]/?search=[Search request]
?