'Getting `[Object: null prototype]` with Fastify, expected values are undefined

I register the following plugins on server boot:

const registerPlugins = (app) => {
    app.register(require('fastify-cors'), {
        origin: '*',
        methods: ['POST', 'PUT', 'DELETE'],
        credentials: true,
    });

    app.register(require('fastify-static'), {
        root: path.join(__dirname, 'client', 'build'),
    });

    app.register(require('fastify-helmet'), { contentSecurityPolicy: false });
    app.register(require('fastify-multer').contentParser);
    app.register(require('fastify-formbody'));
    app.register(require('fastify-qs'), {});
};

And I have a simple route that looks like this:

fastify.post('/login', {
    handler,
})

And my handler looks like this:

const handler = async (req, reply) => {
    try {
        const { email, password } = req.body;

        console.log('req.body', req.body);
        console.log('email', email);
        console.log('password', password);

        return reply.send();
    } catch (err) {
        return reply.code(500).send({ error: err });
    }
};

email and password are undefined for some reason, but when I print req.body, it gives me this:

req.body [Object: null prototype] {
   '{"email":"asdasd","password":"asdas"}': ''
}

What am I doing wrong here?



Sources

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

Source: Stack Overflow

Solution Source