I have a class that wraps a mongodb client for node.js. The the class below when I call findUsers I get that this.collection is undefined.
How do I access this.collection from the prototype?
Thank you!
Class:
var Users;
Users = (function () {
function Users(db) {
db.collection('users', function (err, collection) {
this.collection = collection;
});
}
Users.prototype.findUsers = function (callback) {
this.collection.find({}, function (err, results) {
});
}
return Users;
})();
Usage:
//db holds the db object already created
var user = new Users(db);
user.findUsers();
You are doing it right in the prototype method, your error is in the callback function of
db.collection().