Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

uncaught: TypeError: Cannot read property 'databaseName' of undefined error using mongoose-dbref .0.2 with mongoose 1.5 #5

jessesanford opened this Issue Jul 5, 2011 · 2 comments


None yet
2 participants

I am trying to unit test some models with dbrefs and I am getting the following error when trying to dereference a dbref type.

uncaught: TypeError: Cannot read property 'databaseName' of undefined
at model.setControllingPlayer (/Users/jesse/workspace/BigClawServer/trunk/node_modules/mongoose-dbref/lib/plugins/resolveDBRefs.js:105:46)
at /Users/jesse/workspace/BigClawServer/trunk/test/unit/MissionInstance.js:82:23
at handleError (/Users/jesse/workspace/BigClawServer/trunk/node_modules/mongoose/node_modules/hooks/hooks.js:92:18)
at /Users/jesse/workspace/BigClawServer/trunk/node_modules/mongoose/node_modules/hooks/hooks.js:34:22
at fnWrapper (/Users/jesse/workspace/BigClawServer/trunk/node_modules/mongoose/node_modules/hooks/hooks.js:158:8)
at /Users/jesse/workspace/BigClawServer/trunk/node_modules/mongoose/lib/mongoose/document.js:521:30
at validate (/Users/jesse/workspace/BigClawServer/trunk/node_modules/mongoose/lib/mongoose/schematype.js:239:7)
at /Users/jesse/workspace/BigClawServer/trunk/node_modules/mongoose/lib/mongoose/schematype.js:255:9
at Array.forEach (native)
at SchemaString.doValidate (/Users/jesse/workspace/BigClawServer/trunk/node_modules/mongoose/lib/mongoose/schematype.js:244:19)

the code producing the error is

var     should = require('should')
      , mongoose = require('mongoose')
      , MissionInstanceSchema = require('../../models/MissionInstance')
      , UserSchema = require('../../models/MissionInstance')
      , Schema = mongoose.Schema
      , SchemaType = mongoose.SchemaType
      , ValidatorError = SchemaType.ValidatorError
      , DocumentObjectId = mongoose.Types.ObjectId
      , MongooseError = mongoose.Error;

var db = mongoose.createConnection("mongodb://");
var dbref = require("mongoose-dbref");
var utils = dbref.utils;
var loaded = dbref.install(mongoose);

var sys = require('sys');

 * Simple expresso tests for the MissionInstance model
module.exports = {
  'Test that an Mission Instance has a DBRef to a User': function(){

        var MissionInstance = mongoose.model('MissionInstance');

        var missionInstance = new MissionInstance();

        var User = mongoose.model('User');

        var user = new User();


                    console.log('the user is now set to', controllingPlayer);
                    console.log('the type of the user is', typeof controllingPlayer);



and here is the schema

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ObjectId = Schema.ObjectId;
    /*log4js = require('log4js')(),
    log = log4js.getLogger('user-model'),
    pit = require('../resources/contentpit.js').pit;

var Mission = require('./Mission');
var Item = require('./Item');
var Squad = require('./Squad');
var User = require('./User');

var dbref = require("mongoose-dbref");
var utils = dbref.utils;
var loaded = dbref.install(mongoose);
var DBRef = mongoose.SchemaTypes.DBRef;

var MissionInstance = new Schema({
    description: {type: String, required: true}, //confirm that this is required
    template: {type: String, required: true}, //aka prototype
    controllingPlayer: {type: DBRef, resolve: true},
    controllingArmy:  [Squad],
    controllingHeroItems: [Item],
    controllingDragonItems: [Item],
    playerRecords: [{type: DBRef, resolve: true}] //maybe this should be a dbref?

mongoose.model('MissionInstance', MissionInstance);
module.exports = MissionInstance;

There was an issue with my db connection. specifically in the code above references to mongoose.model('...'); should have been db.model('...');


goulash1971 commented Jul 5, 2011

Yes .. just saw that ... but have updated resolveDbRefs in any case so that the name of the database is taken from the connection object (rather than the underlying database) when the setter is called and added a check to ensure that the connection is open when the getter is called.

Let me know if you have any problem with it and I can roll it back.

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