Boss Machine Project: Can't get the array of minions

Hello everyone,

I hope all is well. I am currently working on the Boss Machine project. Here is the link to the project.

I am currently working on the first part, which is trying to fetch all the minions in the array. Here is my code so far:

server.js:

const express = require('express');
const app = express();
const bodyParser =  require('body-parser');
const cors = require('cors');

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
app.use(cors());

// Add middleware for parsing request bodies here:
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(`PORT is listening on ${PORT}`)
  });
}

Here is my minions.js, where I write the GET route to retrieve the minions array:

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

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

    minionsRouter.get('/minions',(res,req,next) => {
      const allMinions = getAllFromDatabase('minions');
      res.status(200).send(allMinions);
  })

For some reason I fail the test when I run it. I’m not sure why. Perhaps it could be the path I wrote which is wrong. Any ideas? Any help is appreciated. Thanks :slight_smile:

in case anyone is wondering, this is the error I am receiving:

Hi everyone,

I just figured out what I was doing wrong. I forgot to mount my minionsRouter to a path. So this is what I did to resolve the issue. Here you can see what I did in my api.js:

const express = require('express');
const app = require('../server');
const apiRouter = express.Router();

//The code below mounts minionsRouter to a path
const minionsRouter = require('./minions');
apiRouter.use('/minions',minionsRouter);

module.exports = apiRouter;

And then I had to export my minionsRouter. Here is my revised minions.js:

const express = require('express');
const minionsRouter = express.Router();
const {
    createMeeting,
    getAllFromDatabase,
    getFromDatabaseById,
    addToDatabase,
    updateInstanceInDatabase,
    deleteFromDatabasebyId,
    deleteAllFromDatabase,
  } = require('./db');

    //path is now '/' because I mounted apiRouter to '/api' and I mounted minionsRouter to '/minions'
    minionsRouter.get('/',(req,res,next) => {
      const allMinions = getAllFromDatabase('minions');
      res.status(200).send(allMinions);
  });

//exported minionsRouter below
module.exports = minionsRouter;

Well I hope this helps anyone that had the same problem as me. Cheers :slight_smile:

Oh and here are the test results:
Screen Shot 2021-11-22 at 12.45.26 PM