serverless-next.js icon indicating copy to clipboard operation
serverless-next.js copied to clipboard

API Lambda: TypeError: Cannot read property 'send' of undefined

Open thiskevinwang opened this issue 3 years ago • 2 comments

Issue Summary

Given an api route, /api/ga-proxy, with the following function code

import { NextApiHandler } from "next";

const handler: NextApiHandler = async (req, res) => {
  const url = new URL(req.query.url as string);

  const gaResponse = await fetch(url.toString());
  const gaResponseText = await gaResponse.text();

  return res
    .status(200)
    .setHeader("Content-Type", "text/javascript")
    .send(gaResponseText);
};

export default handler;

I get a 503 from CloudFront, I see the following logs for the API Lambda

2022-02-03T04:37:13.618Z	cbdbe942-552a-4b52-9eb2-5726fa29cf4e	ERROR	TypeError: Cannot read property 'send' of undefined
    at handler (/var/task/pages/api/ga-proxy.js:172:72)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async Object.apiResolver (/var/task/chunks/458.js:5607:9)
    at async Module.<anonymous> (/var/task/chunks/458.js:3177:13)

Actual behavior

TBD

Expected behavior

TBD

Steps to reproduce

TBD

Screenshots/Code/Configuration/Logs

Versions

  • OS/Environment: ---
  • @sls-next/serverless-component version: 3.7.0-alpha.7
  • Next.js version: 12.0.8

Additional context

Checklist

  • [ ] You have reviewed the README and FAQs, which answers several common questions.
  • [ ] You have reviewed our DEBUGGING wiki and have tried your best to include complete information and reproduction steps (including your configuration) as is possible. As there is only one maintainer (who maintains this in his free time) and thus very limited resources, if you have time, please try to debug the issue a bit yourself if possible.
  • [x] You have first tried using the most recent latest or alpha @sls-next/serverless-component release version, which may have already fixed your issue or implemented the feature you are trying to use. Note that the old serverless-next.js component and the serverless-next.js plugin are deprecated and no longer maintained.

thiskevinwang avatar Feb 03 '22 04:02 thiskevinwang

Looks like a workaround is to not use the fluent api (method chaining)

-  return res
-    .status(200)
-    .setHeader("Content-Type", "text/javascript")
-    .send(gaResponseText);
+  res.setHeader("Content-Type", "text/javascript");
+  return res.status(200).send(gaResponseText);

thiskevinwang avatar Feb 03 '22 04:02 thiskevinwang

Same issue here with method chaining.

@sls-next/serverless-component version: 3.7.0 Next.js version: 12.1.6 Lambda runtime 14.x or 16.x

soonoo avatar May 14 '22 17:05 soonoo