Newbie struggling with functions and parameters

37 views
Skip to first unread message

Victor Zambrano

unread,
Jun 21, 2016, 12:09:26 AM6/21/16
to nodejs
In my journey to learns Javascript, i see often things like this:

Express.js
router.get('/', function (req, res) {
  res
.send('Hello World');

The method get is passing parameters (req, res, but there are more) to the anonymous function. Reading the Express.js API could find something about. How do I know which parameters are usually passed back ? What am I missing here ?

Another example, from  MongoDB:
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
 
console.log("Connected correctly to server");

Again, reading the API, could not find something about what the connect method pass back to the anonymous function , in this case err and db. What am I missing about the Javascript basic concepts ?

Thanks in advance.

Ryan Schmidt

unread,
Jun 21, 2016, 12:38:00 AM6/21/16
to nod...@googlegroups.com

On Jun 20, 2016, at 11:08 PM, Victor Zambrano wrote:

> In my journey to learns Javascript, i see often things like this:
>
> Express.js
> router.get('/', function (req, res) {
> res.send('Hello World');
>
> The method get is passing parameters (req, res, but there are more) to the anonymous function. Reading the Express.js API could find something about. How do I know which parameters are usually passed back ? What am I missing here ?

From the Express documentation for router.param:

http://expressjs.com/en/4x/api.html#router

> The parameters of the callback function are:
>
> • req, the request object.
> • res, the response object.
> • next, indicating the next middleware function.
> • The value of the name parameter.
> • The name of the parameter.

That's not exactly the same as router.METHOD, which only has req, res and next. I'm not sure why the Express documentation doesn't point that out explicitly. You could point that documentation deficiency out to the Express developers.


> Another example, from MongoDB:
> MongoClient.connect(url, function(err, db) {
> assert.equal(null, err);
> console.log("Connected correctly to server");
>
> Again, reading the API, could not find something about what the connect method pass back to the anonymous function , in this case err and db. What am I missing about the Javascript basic concepts ?

From the documentation:

https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html#connect

> • callback (function) – this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the initialized db object or null if an error occured.

Reply all
Reply to author
Forward
0 new messages