-1

I am trying to parse data from the client to the server, but somehow I'm getting undefined for the data. Can you give your thought on why this is behaving this way?

const handler = async (req, res) => {
    const {data} = req.body;
    if (req.method === "POST") {
        try {
            console.log(data) // Retuned `undefined`
            res.status(200).json({ message: "okay" })
        } catch (error) {
            console.error("Error occurred:", error.message);
            res.status(500).json({ error: "An error occurred while processing your request." });
        }
    }
};

export default handler;
Xhris
  • 55
  • 7
  • Have you loaded the appropriate middleware to parse it? – Barmar Aug 07 '23 at 21:39
  • Hello Barmar, Yes sir. I loaded all the middleware necessary for the page to work. It's really weird cos I have done this a million times making a POST request to the server. It return Status code 200, its just not rendering the data. Thanks for your response, please let me know if you have any more suggestions – Xhris Aug 07 '23 at 23:09
  • What do you see if you do `console.log(req.body);`? – Barmar Aug 07 '23 at 23:24
  • I got `undefined` when I logged it to the console – Xhris Aug 08 '23 at 01:24
  • If there's no `req.body`, it sounds like you don't really have body-parser middleware configured. – Barmar Aug 08 '23 at 15:21

0 Answers0