'Nextjs TypeError: "Cannot read property 'headers' of undefined", after update to nextjs 12.1.0

After update to Nextjs 12.1.0, when I call api via api route, the following error is returned. I'm using aws amplify.

The following error is returned in the CloudFront console:

My api route:

const handlerProducts = async (req: NextApiRequest, res:NextApiResponse) => {
const params = req.query;

try {
  const { data } = await axios.get(URL, {
    params,
  });

  res.status(200).send(data);
} catch (err: any) {
  res.status(500).end();
}};

What could be causing this problem? Thanks for all the help.



Solution 1:[1]

I downgraded next.js to 12.0.8 and it works again

While investigating possibly the same issue, I found https://github.com/serverless-nextjs/serverless-next.js/issues/2327

zhenjie states on GitHub: "Confirmed 12.0.8 works fine, and 12.0.9 does not work."

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alexander Thiel