Jacob Clark
Jacob Clark

Reputation: 3447

Callback Undefined is not a Function

I am attempting to reuse a MongoDB connection again through the use of callbacks, however, when I attempt to invoke my callback I am receiving undefined is not a function.

Can anybody point me in the correct direction as to where I may be going wrong

BroadbandData.prototype.connectToMongoDB = function(callback, obj){
    MongoClient.connect('mongodb://127.0.0.1:27017/UKBroadbandCoverageAndSpeed', function(err, db) {
        this.mongoDB = db;
        callback(db, obj);
    });
}


BroadbandData.prototype.storeMongoRecord = function(db, obj){
    console.log("Hello World")
}

/*
* persist
* @params Array (Object), String
* @returns null
*/
BroadbandData.prototype.persist = function(obj, source){
    if(source == 'mongodb'){

        this.connectToMongoDB(this.storeMongoRecord(), obj);

        /*var collection = db.collection('data');
        collection.insert(obj, function(err, docs) { 
            if(err) throw(err); 
            console.log("Inserted")
        });
*/  
    }else if(source == 'object'){
        this.broadbandDataJSON.push(obj);
    }

}

Upvotes: 0

Views: 482

Answers (1)

SLaks
SLaks

Reputation: 887469

connectToMongoDB(this.storeMongoRecord(), ...

You just called the function, and passed the result (which is undefined) to connectToMongoDB. (just like any other function call)

Upvotes: 2

Related Questions