Routes
const express = require('express');
const { check } = require('express-validator');
const logControllers = require('../controllers/log-controllers');
const router = express.Router();
router.post('/',
[
check('title')
.not()
.isEmpty()
],
logControllers.createLogEntry
);
logControllers
const uuid = require('uuid/v4');
const { validationResult } = require('express-validator');
const HttpError = require('../models/http-error');
let DUMMY_LOGENTRIES = [
{
id: "j1",
title: "king"}]
const createLogEntry = (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
console.log(errors);
throw new HttpError('Invalid inputs passed, please check your data.', 422);
}
const { title } = req.body;
const createdLogEntry =
//new logEntry(
{
id: uuid(), title
};
DUMMY_LOGENTRIES.push(createdLogEntry);
res.status(201).json({ logEntry: createdLogEntry });
};
When I remove validators, POST request works, but with the validator Nodemon returns:
formatter: [Function: formatter],
errors: [
{
value: undefined,
msg: 'Invalid value',
param: 'title',
location: 'body'
},
Bonus question: Without the validator the returned response is only the uuid... not the title and I can't figure out why since I ask for the full createdLogEntry which should return title and uuid. Thoughts?
Didn't find what was missing in terms of the validator, but I did find that I was returning text instead of JSON in Postman. Just rewrote everything and it worked. Guessing it was syntax.