1

I am using NodeJS together with MongoDB and have some issues with passing the mongoDB object to all my prototype functions. I don't understand how to pass this object between these prototypes. Maybe someone could point me in the right direction?

In my main file I create a new instance of my mongoDB object which contains all the prototypes I want to use to work with mongoDB. Then I use the prototype functions to connect and to create a new collection.

Main.js

var mongo = require('./database/mongoDB')
var mongoDB = new mongo();
// Connect to database
mongoDB.ConnectDB(dbPath);
// Create a collection
mongoDB.CreateNewCollection("Usernames");

The prototype functions are defined in MongoDB.js

MongoDB.js
// Get mongoDB

var mongoDB = require('mongodb').MongoClient;
var DatabaseOperations = function () {}; // Constructor

DatabaseOperations.prototype.ConnectDB = function(dbPath){
// Connect to database
mongoDB.connect(dbPath, function(err, mongoDatabase) {
if(!err) {
  console.log("Connected to database: " + dbPath);
  mongoDB.database = mongoDatabase;
} else {
  console.log("Could not connect to database, error returned: " + err);
}
});
}

DatabaseOperations.prototype.CreateNewCollection =   function(collectionName){
mongoDB.database.createCollection(collectionName, function(err,  collectionName){
if(!err) {
  console.log("Successfully setup collection: " + collectionName.Username);
  mongoDB.collectionName = collectionName;
} else {
  console.log("Could not setup collection, error returned: " + err);
}
});
}

I am able to connect to the database but from there on, I do not know how to pass the database object to other prototype functions in order to create a collection or do anything else with it. The error message I get when running it is this:

mongoDB.database.createCollection(collectionName, function(err, collection
TypeError: Cannot read property 'createCollection' of undefined

How do I get the database object into each of the prototype functions to work with it?

2
  • Do you call DatabaseOperations.prototype.CreateNewCollection before the asynchronous callback for mongoDB.connect has executed? Commented Jun 22, 2015 at 0:59
  • Hi Patrick, no I don't call it before the callback, I first call mongoDB.connect() after that I am calling the CreateNewCollections prototype. I try to save the new database object called mongoDatabase in mongoDB.database, but from there on I cannot access it, not through a global variable and also not by returning the value to main.js and pass it to CreateNewCollections.prototype as an argument ... Commented Jun 22, 2015 at 23:49

2 Answers 2

1

I see you found an alternative solution, but I'm going to comment anyway. Inside the constructor you should be doing something like

MongoDB.js
// Get mongoDB

var mongoDB = require('mongodb').MongoClient;
var DatabaseOperations = function () {}; // Constructor

DatabaseOperations.prototype.ConnectDB = function(dbPath){
// Connect to database
var that = this;
mongoDB.connect(dbPath, function(err, mongoDatabase) {
if(!err) {
  console.log("Connected to database: " + dbPath);
  that.database = mongoDatabase;
} else {
  console.log("Could not connect to database, error returned: " + err);
}
});
}

DatabaseOperations.prototype.CreateNewCollection =   function(collectionName){
this.database.createCollection(collectionName, function(err,  collectionName){
if(!err) {
  console.log("Successfully setup collection: " + collectionName.Username);
} else {
  console.log("Could not setup collection, error returned: " + err);
}
});
}

This way, you are assigning the database to the DatabaseOperations, and every prototype function of DatabaseOperations will have access to it through this.database. The database is now a property of the object. In general, different functions can only see stuff assigned to 'this' (DatabaseOperations).

Sign up to request clarification or add additional context in comments.

3 Comments

Ok that makes sense to me, however, why can't I just do: "this.database = mongoDatabase"? Wouldn't it assign the database object as a property of the class object as well?
Actually not. It's about the context. Because mongoDB.connect is async, its callback function will be called with the context being the mongoDB object, rather than the DatabaseOperations object, to which we want to assign the property. In general its a standar scheme to do "var that = this", so that you can access the object inside async callbacks. Edit: To make it more clear 'this' inside mongoDB.connect === mongoDB
Thanks for the explanation, this makes sense to me now! ;-)
0

I ended up using mongoskin as a driver for my Mongo database. By using mongoskin I was able to get a valid mongodb object to work with my collections in my prototype functions: http://www.hacksparrow.com/mongoskin-tutorial-with-examples.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.