Next.js Discord

Discord Forum

Request and response is undefined in API route

Unanswered
Northeast Congo Lion posted this in #help-forum
Open in Discord
Northeast Congo LionOP
I'm creating an API route since I think that's the best way to accomplish this.

this is my code (src/app/seen/page.ts):
import type { NextApiRequest, NextApiResponse } from 'next'
 
export default function handler(req: NextApiRequest, res: NextApiResponse) {
    if (req.method === 'POST') {
        // Process a POST request
        console.log(req,res)
        res.status(200).json({ 'msg': 'hi POST!' })
    } else {
        // Handle any other HTTP method
        console.log(req,res)
        res.status(200).json({ 'msg': 'hi non-POST!' })
    }
}

When I had a basic tsx file in the folder, it ran fine so I don't think it's an issue with it being there, and the code is executed.

The error is
{ params: {}, searchParams: {} } undefined
 ⨯ src\app\seen\page.ts (11:12) @ status
 ⨯ TypeError: Cannot read properties of undefined (reading 'status')
    at handler (./src/app/seen/page.ts:15:13)
    at stringify (<anonymous>)
   9 |         // Handle any other HTTP method
  10 |         console.log(req,res)
> 11 |         res.status(200).json({ 'msg': 'hi non-POST!' })
     |            ^
  12 |     }
  13 | }

2 Replies