While working on my frontend project, I encountered an issue with a simple API call using API routes. Whenever I try to complete the call, I face the following error which prevents my redirect from working:
API resolved without sending a response for /api/uploadLog, this may result in stalled requests.
Despite going through solutions provided by other developers, I am still unable to resolve the issue and feel like I might be missing something critical in the underlying process.
The non-working code snippet is as follows:
export default function handler(req: NextApiRequest, res: NextApiResponse) {
fs.readdir('./src/data/logs', (err, files) => {
if (!err) {
const logNumber = files.length;
fs.writeFileSync(`./src/data/logs/log${logNumber}.txt`, req.body);
res.status(200).json({ message: `log${logNumber}` });
} else {
res.status(500).json({ message: err });
}
});
};
However, the following code does work:
export default function handler(req: NextApiRequest, res: NextApiResponse) {
fs.readdir('./src/data/logs', (err, files) => {
if (!err) {
const logNumber = files.length;
fs.writeFileSync(`./src/data/logs/log${logNumber}.txt`, req.body);
} else {
res.status(500).json({ message: err });
}
});
res.status(200).json({ message: `Hello World!` });
};
Unfortunately, I cannot use the successful approach because I need to address the readdir first.
I would greatly appreciate any assistance or insights into this matter.