user3340037
user3340037

Reputation: 731

Express: How does this function work?

  router.post('/register', function(req, res) {
    User.register(new User({ username : req.body.email }), req.body.password, function(err, account) {
        if (err) {
            return res.render('register', { account : account });
        }

        passport.authenticate('local')(req, res, function () {
          res.redirect('/');
        });
    });
  });

I get the general idea of this routing post, but I don't understand all of it.

What information generally comes in through req? It seems like if I do req.body.email and if in the body of my register html page I've submitted a form with an email and password field, I can simply access them this way?

And in the function(err, account) callback, where exactly is it getting the err and account variables? I've never really understood for the callback functions how the variables were decided or what even they are, it seems like when you route it somehow takes two variables err and account?

Thanks so much!

Upvotes: 0

Views: 70

Answers (2)

Dayan Moreno Leon
Dayan Moreno Leon

Reputation: 5435

1st- Well you should read the ExpressJS documentation and see the difference between body, query and params but basically goes like this

body refers to the body of the request, which is the submitted data through POST or PUT query refers to the search part of the url or query string everything after the "?" params refers to the part of the path that is parameterized.

hope that gives you a clue of how and where to look for the information

2nd- is nodes convention that always the first variables passed to a callback is the error variablem the rest is according to the function in this case is account because the function is suppose to create db account and return the entire account information so ir can bu used by the callback

Upvotes: 1

Jorge Aranda
Jorge Aranda

Reputation: 2080

What information generally comes in through req?

The request object (req) comes with data on the request you are processing. For instance:

  • Fields used in the form that triggered the request (under req.body)
  • Parameters used in the request URL (under req.params)
  • Session/cookie information (under req.session)

See here for more.

And in the function(err, account) callback, where exactly is it getting the err and account variables?

By convention in Node, callbacks pass any errors as the first parameter, and any non-error results from the second parameter onwards. So you'll typically see the first parameter called err. The structure of the err object is not completely standard, but it is normal to assume there will be a message field in the err object, and perhaps some error code.

In your example, you are handing that callback to, it seems, Mongoose, or some other database handling library. This library will try to execute a register function on the User object/model/schema. When it's done, if it encountered any errors, they'll come back to you on the err object. Else, you can expect the account object to hold details on the user account.

Also: you could name err and account whatever you want, of course.

Upvotes: 0

Related Questions