I wrote a function to get posted form data in a NextJs page. While this works, the req and res parameters aren't typed:
const getBody = promisify(bodyParser.urlencoded());
export async function getServerSideProps({ req, res }) {
if (req.method === "POST") {
await getBody(req, res);
}
return {
props: {
input: req.body?.input
}
}
}
I've tried using req: NextApiRequest, res: NextApiResponse with an interface but the function body won't work:
interface ExtendedNextApiRequest extends NextApiRequest {
body: {
input: string;
};
}
export async function getServerSideProps(req: ExtendedNextApiRequest, res: NextApiResponse) {
if (req.method === "POST") {
await getBody(req, res);
}
return {
props: {
input: req.body?.input
}
}
}
However I get a serialization error:
Error: Error serializing `.input` returned from `getServerSideProps` in "/my-page".
Reason: `undefined` cannot be serialized as JSON. Please use `null` or omit this value.
<input name="input" defaultValue={props.input} />