Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created January 14, 2013 18:46
Show Gist options
  • Save aheckmann/4532288 to your computer and use it in GitHub Desktop.
Save aheckmann/4532288 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var assert = require('assert')
console.log('\n===========');
console.log(' mongoose version: %s', mongoose.version);
console.log('========\n\n');
var schema = new Schema({
name: String
});
schema.statics.doStuff = function () {
console.log('doStuff');
this.doOtherStuff();
}
schema.statics.doOtherStuff = function () {
console.log('doOtherStuff');
}
var A = mongoose.model('A', schema);
A.doStuff();
@mcauser
Copy link

mcauser commented Jan 14, 2013

Hi Aaron,
Thanks for that.
I had incorrectly referred to this.doOtherStuff() inside an anonymous function in doStuff().
The this scope was not the right one...
eg.

schema.statics.doStuff = function(id, callback) {
    this.findOne(id, function(err, item) {
        this.doOtherStuff(item, function(err, final) {
            callback(null,final);
        });
    });
}

My understanding is this.doOtherStuff() refers to the this scope of the anonymous function function(err,item) {} instead of the expected function(id,callback) {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment