I'm trying to figure out how to pass in a value for an optional parameter, but the issue is that 'undefined' is being treated as a string instead of the way I want it to behave. Any ideas on what I might be missing here? The default route should be /api/todo/get/ and I know I could just omit 'showAll', but I'd prefer to avoid using an awkward if else construct. Thanks!
const getTodos = (showAll = undefined): Thunk<Promise<void>> => (dispatch) => {
dispatch(setTodos([]));
return axios.get(`/api/todo/get/${showAll}`).then((res) => {
dispatch(setTodos(res.data));
});
};
router.get("/get/:includeCompleted?", (req: Request, res: Response) => {
const includeCompleted = req.params.includeCompleted;
console.log(req.params.includeCompleted);
return database
.raw<Todo[]>(
`
SELECT *
FROM todo
WHERE :includeCompleted = 1 OR completedDate IS NULL
`,
{includeCompleted: includeCompleted ? 1 : 0}
)
.then((data) => res.status(StatusCodes.OK).json(data));
});