4 fails on the run test and I do not understand why it’s sending a 400 bad request when I specified the status for each route error handling.
TERMINAL:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
module.exports = app;
const PORT = process.env.PORT || 4001;
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const {
createMeeting,
getAllFromDatabase,
getFromDatabaseById,
addToDatabase,
updateInstanceInDatabase,
deleteFromDatabasebyId,
deleteAllFromDatabase,
} = require('./server/db');
const checkMillionDollarIdea = require('./server/checkMillionDollarIdea');
// Middleware to validate ID parameter
const validateIdParam = (paramName) => {
return (req, res, next) => {
const id = req.params[paramName];
if (!id || isNaN(id)) {
return res.status(404).send();
}
next();
};
};
// *** MINION ROUTES ***
app.get('/api/minions', (req, res) => {
const minions = getAllFromDatabase('minions');
res.send(minions);
});
app.post('/api/minions', (req, res) => {
const newMinion = addToDatabase('minions', req.body);
res.status(201).send(newMinion);
});
app.get('/api/minions/:minionId', validateIdParam('minionId'), (req, res) => {
const minion = getFromDatabaseById('minions', req.params.minionId);
if (!minion) {
return res.status(404).send();
}
res.send(minion);
});
app.put('/api/minions/:minionId', validateIdParam('minionId'), (req, res) => {
const updatedMinion = updateInstanceInDatabase('minions', req.body);
if (!updatedMinion) {
return res.status(404).send();
}
res.send(updatedMinion);
});
app.delete('/api/minions/:minionId', validateIdParam('minionId'), (req, res) => {
const deleted = deleteFromDatabasebyId('minions', req.params.minionId);
if (!deleted) {
return res.status(404).send();
}
res.status(204).send();
});
// *** IDEA ROUTES ***
app.get('/api/ideas', (req, res) => {
res.send(getAllFromDatabase('ideas'));
});
app.get('/api/ideas/:ideaId', validateIdParam('ideaId'), (req, res) => {
const idea = getFromDatabaseById('ideas', req.params.ideaId);
if (!idea) {
return res.status(404).send();
}
res.send(idea);
});
app.post('/api/ideas', checkMillionDollarIdea, (req, res) => {
const newIdea = addToDatabase('ideas', req.body);
res.status(201).send(newIdea);
});
app.put('/api/ideas/:ideaId', validateIdParam('ideaId'), checkMillionDollarIdea, (req, res) => {
const updatedIdea = updateInstanceInDatabase('ideas', req.body);
if (!updatedIdea) {
return res.status(404).send();
}
res.send(updatedIdea);
});
app.delete('/api/ideas/:ideaId', validateIdParam('ideaId'), (req, res) => {
const deleted = deleteFromDatabasebyId('ideas', req.params.ideaId);
if (!deleted) {
return res.status(404).send();
}
res.status(204).send();
});
// *** MEETING ROUTES ***
app.get('/api/meetings', (req, res) => {
res.send(getAllFromDatabase('meetings'));
});
app.post('/api/meetings', (req, res) => {
const newMeeting = createMeeting();
addToDatabase('meetings', newMeeting);
res.status(201).send(newMeeting);
});
app.delete('/api/meetings', (req, res) => {
deleteAllFromDatabase('meetings');
res.status(204).send();
});
if (!module.parent) {
app.listen(PORT, () => {
console.log(`Server is running on port: ${PORT}`);
});
}
SERVER.JS FILE
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
module.exports = app;
const PORT = process.env.PORT || 4001;
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const {
createMeeting,
getAllFromDatabase,
getFromDatabaseById,
addToDatabase,
updateInstanceInDatabase,
deleteFromDatabasebyId,
deleteAllFromDatabase,
} = require('./server/db');
const checkMillionDollarIdea = require('./server/checkMillionDollarIdea');
// Middleware to validate ID parameter
const validateIdParam = (paramName) => {
return (req, res, next) => {
const id = req.params[paramName];
if (!id || isNaN(id)) {
return res.status(404).send();
}
next();
};
};
// *** MINION ROUTES ***
app.get('/api/minions', (req, res) => {
const minions = getAllFromDatabase('minions');
res.send(minions);
});
app.post('/api/minions', (req, res) => {
const newMinion = addToDatabase('minions', req.body);
res.status(201).send(newMinion);
});
app.get('/api/minions/:minionId', validateIdParam('minionId'), (req, res) => {
const minion = getFromDatabaseById('minions', req.params.minionId);
if (!minion) {
return res.status(404).send();
}
res.send(minion);
});
app.put('/api/minions/:minionId', validateIdParam('minionId'), (req, res) => {
const updatedMinion = updateInstanceInDatabase('minions', req.body);
if (!updatedMinion) {
return res.status(404).send();
}
res.send(updatedMinion);
});
app.delete('/api/minions/:minionId', validateIdParam('minionId'), (req, res) => {
const deleted = deleteFromDatabasebyId('minions', req.params.minionId);
if (!deleted) {
return res.status(404).send();
}
res.status(204).send();
});
// *** IDEA ROUTES ***
app.get('/api/ideas', (req, res) => {
res.send(getAllFromDatabase('ideas'));
});
app.get('/api/ideas/:ideaId', validateIdParam('ideaId'), (req, res) => {
const idea = getFromDatabaseById('ideas', req.params.ideaId);
if (!idea) {
return res.status(404).send();
}
res.send(idea);
});
app.post('/api/ideas', checkMillionDollarIdea, (req, res) => {
const newIdea = addToDatabase('ideas', req.body);
res.status(201).send(newIdea);
});
app.put('/api/ideas/:ideaId', validateIdParam('ideaId'), checkMillionDollarIdea, (req, res) => {
const updatedIdea = updateInstanceInDatabase('ideas', req.body);
if (!updatedIdea) {
return res.status(404).send();
}
res.send(updatedIdea);
});
app.delete('/api/ideas/:ideaId', validateIdParam('ideaId'), (req, res) => {
const deleted = deleteFromDatabasebyId('ideas', req.params.ideaId);
if (!deleted) {
return res.status(404).send();
}
res.status(204).send();
});
// *** MEETING ROUTES ***
app.get('/api/meetings', (req, res) => {
res.send(getAllFromDatabase('meetings'));
});
app.post('/api/meetings', (req, res) => {
const newMeeting = createMeeting();
addToDatabase('meetings', newMeeting);
res.status(201).send(newMeeting);
});
app.delete('/api/meetings', (req, res) => {
deleteAllFromDatabase('meetings');
res.status(204).send();
});
if (!module.parent) {
app.listen(PORT, () => {
console.log(`Server is running on port: ${PORT}`);
});
}
I was wondering if the checkMillionDollarIdea.js had something to do with the failed test? Code for the PUT & POST routes for minionId and ideaId are identical except for the middleware function:
const checkMillionDollarIdea = (req, res, next) =>
{
const {numWeeks, weeklyRevenue} = req.body;
if (!numWeeks || !weeklyRevenue) {
res.status(400).send('Missing required properties');
return;
}
const week = Number(numWeeks);
const revenue = Number(weeklyRevenue);
if (isNaN(week) || isNaN(revenue)) {
res.status(400).send('Invalid numWeeks or weeklyRevenue');
return;
}
const totalValue = week + revenue;
if (totalValue >= 1000000) {
next();
} else {
res.status(400).send('Must be a million dollar idea');
}
};
// Leave this exports assignment so that the function can be used elsewhere
module.exports = checkMillionDollarIdea;
Can’t wrap my head around this. Any help or tips would be very much appreciated, thank you!