[SOLVED] Express+Knex+req.query

Hi friends, it’s me again

And this time express and knex are beating me a little, can’t make this endpoint work using req.querys (Response from express), I made one with req.params and it was ok.

Express

app.get(`/actor`, async (req: Request, res: Response) => {
	try {
		// const { gender } = req.query;
		const count = await getActorsByGender(req.query.gender as string);

		console.log({ count });
		res.status(200).send({ quantity: count, });
	} catch (error) {
		res.status(200).send({ message: error.sqlMessage || error.message });
	}
});

Knex requisition

const getActorsByGender = async (gender: string): Promise<any> => {
	try {
		const result = await connection.raw(`
			SELECT COUNT(*) as count FROM Actor
			WHERE gender = "${gender}"
		`);

		// console.log(`Temos: ${result[0][0].count} ocorrências`);
		return result;
	} catch (error) {
		console.log(error);
	}
};

Don’t know if this is because of the count(), the knex part is ok, I can console.log the result.
The express part show a empty object on insomnia

Using ‘male’ as parameter it was expected to return “2” as result.

Solved this with some help.

I was using the path param instead of query param in insomnia.

The solution was to take out the /male from the URL PREVIEW as seen in the image on OP, and using the two input under New name and New Value, with query and male, respectively.