Alexis
Alexis

Reputation: 518

Express and mongodb article

im a beginner to Node.js and lately ive tried to analyse the code from:http://howtonode.org/express-mongodb. But im stucked. I really don't understand how is this code works: articleProvider.findAll(function(error, docs){ res.send(docs); });

This method calls the findAll function that looks like this: var articleCounter = 1; ArticleProvider = function(){}; ArticleProvider.prototype.dummyData = [];

ArticleProvider.prototype.findAll = **function(callback) {
 callback( null, this.dummyData )
};**

How can a method findAll to be called with a function callback that isnt even declared?! Thanks for yor help

Upvotes: 0

Views: 276

Answers (3)

Ram Rajamony
Ram Rajamony

Reputation: 1723

The following snippet:

ArticleProvider.prototype.findAll = function(callback)
...

is declaring ArticleProvider.findAll to be a function that itself takes as argument, a function named callback. When you invoke ArticleProvider.findall(foo), the function foo is invoked with two arguments: null as the first argument, and ArticleProvider.dummyData as the second argument.

It may be best to get started with a good Javascript book, such as the Crockford book. Good luck.

[EDIT]

I see the suggestion to use Mongoose in one of the responses below, but after having used it, I now much prefer Guillermo Rauch's Monk. Its much simpler than Mongoose and it lets you work without a schema. For those instances where you just want a simple layer atop MongoDB, Monk did the trick for me.

Upvotes: 4

Hector Correa
Hector Correa

Reputation: 26680

How can a method findAll to be called with a function callback that isnt even declared?!

When you call

articleProvider.findAll(function(error, docs){ res.send(docs); });

you are declaring the function inline, and then passing it as an argument to find all. The "function(error, docs){ res.send(docs); }" is where you are declaring the function and passing it as a parameter.

As Ram indicated, you could have declared the function first (call it foo) and then pass it as a parameter:

var foo = function(error, docs){ res.send(docs); }
articleProvider.findAll(foo);

Notice that in either case you are passing the function as a parameter, not the result of executing the function.

Upvotes: 1

origin1tech
origin1tech

Reputation: 749

Ram did a nice job explaining but I'd add if you are new you might want to take a look at "mongoose" rather than directly working with mongodb. Most new devs find it a little easier and because its widely used (not that mongodb directly isn't but..) you might find it easier to locate samples. here is a quick sample using mongoose.

// require your model
var Product = require('../models/product.js');

// example getting a product by id using mongoose.
app.get('/product/:id, function (req, res) {
      Product.findOne({ _id: id }, function (err, product) {
        if(err) 
           console.log(err + '');
        else
           res.render('product, { title: 'Your Title', model: product });
     });
});

Good Luck!

Upvotes: 1

Related Questions