You are passing req.query to your search and the default Typescript type for req.query is
Request<unknown, unknown, unknown, QueryString.ParsedQs, Record<string, any>>.query: QueryString.ParsedQs
and if you passed in req.query.searchText the type for that would be
string | QueryString.ParsedQs | string[] | QueryString.ParsedQs[] | undefined
The simple way is already answered here just do
export const searching = (req: Request, res: Response) => {
const searchText = req.query. searchText as string
Company.find({ $text: { $search: searchText } }).exec((err, docs) => {
if (docs) {
res.status(200).json(docs)
} else {
console.log(err)
}
})
}
That works but gets sloppy when you have a lot of variables from your query params. In express try out the RequestHandler for your endpoints.
import { RequestHandler } from 'express'
interface QueryTypes {
searchText: string
moreSearchText: string
}
export const searching:RequestHandler<unknown, unknown, unknown, QueryTypes > = (req, res) => {
Company.find({ $text: { $search: req.query } }).exec((err, docs) => {
if (docs) {
res.status(200).json(docs)
} else {
console.log(err)
}
})
}
You can see using the RequestHandler I can pass in a few unknown types and the fourth one I can pass in the query types. I can also remove the Request and Response types to just (req, res) because RequestHandler already types out the handler for us.
You're probably wondering what the 3 previous unknown are and that too in the typescript docs for RequestHandler. Here is the docs.
interface RequestHandler<P = core.ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = qs.ParsedQs, Locals extends Record<string, any> = Record<string, any>>
Now using RequestHandler. I can type out parameters, body, reqbody or query. Use a combo or skip them using unknown like I did in my example.
This also has the added benefit of giving you type safety so
const test = req.query.test
would highlight a typescript error in vscode for me because 'test' is defined in my QueryTypes interface.
No overload matches this call. The last overload gave the following error. Type 'string | ParsedQs | string[] | ParsedQs[] | undefined' is not assignable to type 'string'.
– Compel