I'm working on a tutorial: http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/
I need some help with understanding syntax. Below is the code:
var mongo = require ('mongodb');
var Server = mongo.Server,
Db = mongo.db,
BSON = mongo.BSONPure;
var server = new Server ('localhost', 27017, {auto_reconnet:true});
db = new Db('winedb', server);
db.open(function(err, db) {
if(!err) {
console.log("Connected to 'winedb' database");
//Q1. Why are we passing "collection" ? Where did we declare "collection"?
db.collection('wines', {strict:true}, function(err, collection) {
if (err) {
console.log("The 'wines' collection doesn't exist. Creating it with sample data...");
populateDB();
}
});
}
});
exports.findById = function(req, res)
{
var id = req.params.id;
console.log('Retrieving wine: ' + id);
//Q2. Why do we need to pass function as a parameter? Where is "collection" declared?
db.collection('wines', function(err, collection)
{
collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item)
{
res.send(item);
});
});
}
exports.findAll = function(req, res) {
db.collection('wines', function(err, collection) {
//Q3. Why do we not have to declare "items" anywhere?
collection.find().toArray(function(err, items) {
res.send(items);
});
});
};
Three Questions:
Q1: Why are we passing "collection" ? Where did we declare "collection"?
Q2: Why do we need to pass function as a parameter? Where is "collection" declared?
Q3: Why do we not have to declare "items" anywhere?