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'
});
});
});