Here is a function that retrieves an object from a database and extracts a URL:
async fetchUrl(id: string, redirectFunction: Function) {
if (!IdExists(id)) {
throw new Error(`ID ${id} does not exist.`);
}
const redirectLocation: string = await prisma.url.findUnique({
where: { uniqueId: id },
select: { url: true },
}).then((data) => {
return data?.url!;
});
redirectFunction('http://' + redirectLocation);
}
This function is called within the following code segment:
app.get('/:id', async (req, res) => {
try {
redirectController.fetchUrl(req.params.id, res.redirect);
} catch (error) {
console.error(error);
}
});
There seems to be a
TypeError: Cannot read properties of undefined (reading 'location')
error related to the res.redirect
method. However, when replacing it with console.log
for debugging, the URL is displayed correctly. What could be causing this issue?