Boss Machine Errors

Ok, I give up…
When running ‘’‘npm run test’’’. I am still getting 4 errors -

PUT api/ideas/:ideaId

  1. updates the correct idea and returns it
  2. updates the correct idea and persists to the database

POST api/ideas
3. should add a new idea if all supplied information is correct

checkMillionDollarIdea middleware
4. calls next for ideas that will yield at least one million dollars

My code for ideas.js -

const ideasRouter = require('express').Router();

const {
    addToDatabase,
    getAllFromDatabase,
    getFromDatabaseById,
    updateInstanceInDatabase,
    deleteFromDatabasebyId,
} = require('./db');

const checkMillionDollarIdea = require('./checkMillionDollarIdea');

ideasRouter.param('id', (req, res, next, id) => {
    const idea = getFromDatabaseById('ideas', id);
    if (idea) {
        req.idea = idea;
        next();
    } else {
        res.status(404).send();
    }
});

ideasRouter.get('/', (req, res, next) => {
    res.send(getAllFromDatabase('ideas'));
});

ideasRouter.get('/:id', (req, res, next) => {
    res.send(req.idea);
});

ideasRouter.post('/', checkMillionDollarIdea, (req, res, next) => {
    const newIdea = addToDatabase('ideas', req.body);
    res.status(201).send(newIdea);
});

ideasRouter.put('/:id', checkMillionDollarIdea, (req, res, next) => {
    let updatedIdea = updateInstanceInDatabase('ideas', req.body);
    res.send(updatedIdea);
});

ideasRouter.delete('/:id', (req, res, next) => {
    const deleteIdea = deleteFromDatabasebyId('ideas', req.params.id);
    if(deleteIdea) {
        res.status(204);
    } else {
        res.status(500);
    }
    res.send();
});


module.exports = ideasRouter;

And for checkMillionDollarIdea.js -

const checkMillionDollarIdea = (req, res, next) => {
    const { numOfWeeks, weeklyRevenue } = req.body;
    const total = Number(numOfWeeks) * Number(weeklyRevenue);

    if (!numOfWeeks || !weeklyRevenue || isNaN(total) || total < 1000000) {
        res.status(400).send();
    } else {
        next();
    }
};

// Leave this exports assignment so that the function can be used elsewhere
module.exports = checkMillionDollarIdea;

I would really appreciate it if someone who has completed this project and passed all the tests could see what is missing here. I’ll copy my api.js and server.js files respectively below too.

api.js -

const express = require('express');
const apiRouter = express.Router();

const minionsRouter = require('./minions');
const ideasRouter = require('./ideas');
const meetingsRouter = require('./meetings');

apiRouter.use('/minions', minionsRouter);
apiRouter.use('/ideas', ideasRouter);
apiRouter.use('/meetings', meetingsRouter);



module.exports = apiRouter;

server.js -

const express = require('express');
const app = express();

module.exports = app;

/* Do not change the following line! It is required for testing and allowing
*  the frontend application to interact as planned with the api server
*/
const PORT = process.env.PORT || 4001;

// Add middleware for handling CORS requests from index.html
const cors = require('cors');
app.use(cors());

// Add middware for parsing request bodies here:
const bodyParser = require('body-parser');
app.use(bodyParser.json());

// Mount your existing apiRouter below at the '/api' path.
const apiRouter = require('./server/api');
app.use('/api', apiRouter);

// This conditional is here for testing purposes:
if (!module.parent) { 
  // Add your code to start the server listening at PORT below:
  app.listen(PORT, () => {
    console.log(`Server is listening on port ${PORT}`);
  });
}

Thank you.

/api/ideas

  • GET /api/ideas to get an array of all ideas.
  • POST /api/ideas to create a new idea and save it to the database.
  • GET /api/ideas/:ideaId to get a single idea by id.
  • PUT /api/ideas/:ideaId to update a single idea by id.
  • DELETE /api/ideas/:ideaId to delete a single idea by id.

I’m not sure if this is it, but try using the nomenclature :ideaId ?? The test uses that route to test. Check the others?