0

Receiving the error 'Missing firstName in request field' even when I have entered the request with the firstName key and value filled out. Does anyone have any ideas? I really need the help as I am new to this. Author is a mongoose collection and I am trying to get through the "if" statement and return the list of authors.

app.post('/authors', function (req, res) {
const requiredFields = ['firstName', 'lastName', 'userName'];
requiredFields.forEach(field => {
    if (!(field in req.body)) {
        const message = `Missing \`${field}\` in request body`;
        console.error(message);
        return res.status(400).send(message);
    }
});

Author
    .findOne({
        userName: req.body.userName
    })
    .then(author => {
        if (author) {
            const message = `Username already taken`;
            console.error(message);
            return res.status(400).send(message);
        } else {
            Author
                .create({
                    firstName: req.body.firstName,
                    lastName: req.body.lastName,
                    userName: req.body.userName
                })
                .then(author => res.status(201).json({
                    _id: author.id,
                    name: `${author.firstName} ${author.lastName}`,
                    userName: author.userName
                }))
                .catch(err => {
                    console.error(err);
                    res.status(500).json({
                        error: 'Something went wrong'
                    });
                });
        }
    })
    .catch(err => {
        console.error(err);
        res.status(500).json({
            error: 'Something went really really wrong'
        });
    });
});
Kalana Demel
  • 3,220
  • 3
  • 21
  • 34
Matt
  • 97
  • 1
  • 1
  • 12
  • Please explain what result you are trying to achieve, and what the body of the request is. Also what is `Author`? Is it a Mongoose collection? You may want to mention that in the question. – Aakash Jain Aug 21 '18 at 04:17
  • Thank you I edited the post. Yes Author is a mongoose collection and I am trying to retrieve the list of authors that are in the collection. – Matt Aug 21 '18 at 04:21
  • Did you dump/check `req.body`? What is the value? Are you using the correct [body-parser](https://www.npmjs.org/package/body-parser) middleware? – Tobias K. Aug 21 '18 at 04:36
  • How would I dump/check req.body I have not heard of that. And I tried the body-parser but that did nothing different. when I run the get request I get the collection of authors – Matt Aug 21 '18 at 04:52
  • Possible duplicate of [Cannot POST form node.js - express](https://stackoverflow.com/questions/14902923/cannot-post-form-node-js-express) – Prashant Tapase Aug 21 '18 at 05:40

2 Answers2

1

The solution can be either of below

  1. Check if you have used the body parser correctly (depending on you input),
const express = require('express');
const app = express();
const bodyParser = require('body-parser'); 
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.post('/authors', function (req, res) {
    const requiredFields = ['firstName', 'lastName', 'userName'];
    console.log(req.body);
    requiredFields.forEach(field => {

        if (!(field in req.body)) {
            const message = `Missing \`${field}\` in request body`;
            console.error(message);
            return res.status(400).send(message);
        }
    });
    //Your Other code



});
  1. Make sure you use the following header in your request,

Content-Type : application/json

All conditions satisfied I got the above code working without any issues.

Kalana Demel
  • 3,220
  • 3
  • 21
  • 34
0

If you use Express 4.16.0 or higher u can use this: app.use(express.json());

Vans
  • 115
  • 1
  • 5