-2

I've looked up JavaScript functions and arguments but couldn't find anything to help me understand a function like the one below. You can reference the original tutorial.

createPuppy has three arguments: req, res and next.

function createPuppy(req, res, next) {
  req.body.age = parseInt(req.body.age);
  db.none('insert into pups(name, breed, age, sex)' +
      'values(${name}, ${breed}, ${age}, ${sex})',
    req.body)
    .then(function () {
      res.status(200)
        .json({
          status: 'success',
          message: 'Inserted one puppy'
        });
    })
    .catch(function (err) {
      return next(err);
    });
}

That function is being called by a router:

var express = require('express');
var router = express.Router();

var db = require('../queries');

router.get('/api/puppies', db.getAllPuppies);
router.get('/api/puppies/:id', db.getSinglePuppy);
router.post('/api/puppies', db.createPuppy);
router.put('/api/puppies/:id', db.updatePuppy);
router.delete('/api/puppies/:id', db.removePuppy);
module.exports = router;

When db.createPuppy is called, there wasn't any arguments passed.

How do those three arguments fit into this function?

Update: I'm new to Node, JavaScript, pg-promise and express. So it was a bit overwhelming to narrow down where to dig. I came here to get leads on where to narrow my focus in. Thank you!

Thomas
  • 349
  • 3
  • 11
  • 1
    Read up on Express here: http://expressjs.com/en/guide/routing.html – jmargolisvt Nov 11 '16 at 21:37
  • When a function is *called*, any number of arguments can be passed to it. What you're doing above is *passing* the function as an argument to another function, which will later *call* it with a number of arguments. – Kevin B Nov 11 '16 at 21:57
  • Thank you for your feedback. Your comment led me to seek to understand [JavaScript callback functions](http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/) first; which I clearly didn't understand. I did run across the express routing page you pointed out. Without understanding the callback, that express source didn't help add clarity. Now it is a bit more clear. – Thomas Nov 12 '16 at 00:06

2 Answers2

-1

I believe that (req, res, next) are default arguments in Express.

When you write router.post('/api/puppies', db.createPuppy);, the function createPuppy is not actually called yet. This just establishes what function to call when that method/endpoint is hit.

Express takes care of calling the function and passing in the required arguments to it when you hit the /api/puppies endpoint with a POST.

Hope that helps!

Fernando
  • 280
  • 2
  • 10
  • 1
    You were right with req, res and next being default arguments in express. Eric Kambestad gave the link to express API. Thanks for helping narrow my learning to the right area. – Thomas Nov 12 '16 at 00:30
-1

You will be sending that data through a POST request to that endpoint. You can then access the data you pass in through the req.body variable.

You will also need the bodyParser middleware to access the request body. More on that here.. http://expressjs.com/en/api.html#req.body

  • Your pointer to the express API was very helpful to show that those (req, res and next) functions / objects were detailed in there. I'm in the process of reviewing that entire page. Once I get this down, I'll look into bodyParser too. Thank you. – Thomas Nov 12 '16 at 00:12