Boss Machine solution code clarity

Hey all - I’m looking over the solution code to the Boss Machine Cumulative Project and had a few clarifying questions I was hoping someone could answer. Here goes:

Note: I have not DRY’d this code yet. After examining the DRY’d solution, I went back and rewrote it before DRYing so I could understand the code-writing process. Also, I have called the js file where getFromDatabase() and updateInstanceInDatabase() are located using const database = require('./db')

In nearly every route, there is a bit of request logic I don’t understand: It is the req.minion = minion portion of:

app.put('/api/minions/:minionId', (req, res, next) => {
  const minion = database.getFromDatabaseById('minions', req.params.minionId);
  if (minion) {
    req.minion = minion;
    const updatedMinionInstance = database.updateInstanceInDatabase('minions', req.body);
  } else {

If i’m understanding correction, minion is the object that is pulled using the arguments ‘minion’ and the id specified in the params of the URL. So then we say, if all of that is valid, then… and then I get confused.

Does req.minion mean we now are using the object called using the above function/arguments as the request object? And then, what’s the point of setting it equal to minion again? Is this just sort of converting the function object into our request object?

Then going further, why is req.body being used as the argument in updateInstanceInDatabase()? I realize that’s what we’d typically use, but the req.minion thing threw me off.

Any help is greatly appreciated! Thank you!

apiRouter.put("/minions/:minionId", (req, res) => {
const changeMinion = updateInstanceInDatabase(“minions”, req.body);
changeMinion ? res.send(changeMinion) : res.sendStatus(404);
}); <— it works like this for me also.

req.minion = minion <-- attaches the minion object to the request object aka creates a property on the request object = minion object. Can prove useful sometimes.

I’m still confused about two things: firstly the req.body vs req.minion thing in the PUT method that OP was talking about. How will these two properties differ? My test fails with req.minion but passes with req.body. This is the code for my params method:

minionRouter.param('minionId', (req, res, next, id) => {
    const minion = getFromDatabaseById('minions', id);
    if (!minion) {
        res.status(404).send('Minion not found');
    } else {
    req.minion = minion;

Secondly, why in my code above does the test fail if I provide a first line converting the id parameter of my callback into a number e.g. const minionId = Number(id)?

Hey Tag,

Your code fails when you transform the Id into a number because the getFromDatabaseById accepts a string as an input within the id argument, not a number.


I just delved in and saw they were using template literals to stringify. Thank you!

I worked out my other question for anyone else who is interested: req.minion is actually the minion object from the database, so it’s not the one that is being sent with the request in the first place in any paths.