MongoDB Index Exception

107 views
Skip to first unread message

Fredrick Lackey

unread,
Mar 27, 2015, 9:15:37 AM3/27/15
to node-mong...@googlegroups.com
Original post is at... http://stackoverflow.com/questions/29289637/mongoose-mongodb-index-exception

Has anyone seen the following exception?  Know how to get by it?  My models DO have indexes, so there's no reason, that I can think of, for the indexes not to be present.

The MongoDB client code is throwing an exception within Mongoose...


C:\_\MyProject\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\base.js:246
       throw message;
             ^
TypeError: Cannot read property 'length' of undefined
   at processResults (C:\_\MyProject\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1581:31)
   at C:\_\MyProject\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1619:20
   at C:\_\MyProject\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1157:7
   at C:\_\MyProject\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1890:9
   at Server.Base._callHandler (C:\_\MyProject\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\base.js:448:41)
   at C:\_\MyProject\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\server.js:481:18
   at MongoReply.parseBody (C:\_\MyProject\node_modules\mongoose\node_modules\mongodb\lib\mongodb\responses\mongo_reply.js:68:5)
   at null.<anonymous> (C:\_\MyProject\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\server.js:439:20)
   at emit (events.js:95:17)
   at null.<anonymous> (C:\_\MyProject\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection_pool.js:201:13)

The block of code causing the problem is the iterator in the for loop below...

    /**
     * Retrieves this collections index info.
     *
     * Options
     *  - **full** {Boolean, default:false}, returns the full raw index information.
     *  - **readPreference** {String}, the preferred read preference ((Server.PRIMARY, Server.PRIMARY_PREFERRED, Server.SECONDARY, Server.SECONDARY_PREFERRED, Server.NEAREST).
     *
     * @param {String} collectionName the name of the collection.
     * @param {Object} [options] additional options during update.
     * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occurred, or null otherwise. While the second parameter will contain the results from indexInformation or null if an error occurred.
     * @return {null}
     * @api public
     */
    Db.prototype.indexInformation = function(name, options, callback) {
      if(typeof callback === 'undefined') {
        if(typeof options === 'undefined') {
          callback = name;
          name = null;
        } else {
          callback = options;
        }
        options = {};
      } 
    
      // Throw is no name provided
      if(name == null) throw new Error("A collection name must be provided as first argument");
    
      // If we specified full information
      var full = options['full'] == null ? false : options['full'];
      var self = this;
    
      // Process all the results from the index command and collection
      var processResults = function(indexes) {
        // Contains all the information
        var info = {};
        // Process all the indexes
          if (indexes) {
              for(var i = 0; i < indexes.length; i++) {
                  var index = indexes[i];
                  // Let's unpack the object
                  info[index.name] = [];
                  for(var name in index.key) {
                      info[index.name].push([name, index.key[name]]);
                  }
              }
          }
    
        return info;
      }
    
      // Fallback to pre 2.8 getting the index information
      var fallbackListIndexes = function() {
        // Build selector for the indexes
        var selector = name != null ? {ns: (self.databaseName + "." + name)} : {};
    
        // Get read preference if we set one
        var readPreference = ReadPreference.PRIMARY;
    
        // Iterate through all the fields of the index
        var collection = self.collection(DbCommand.SYSTEM_INDEX_COLLECTION);
        // Perform the find for the collection
        collection.find(selector).setReadPreference(readPreference).toArray(function(err, indexes) {
          if(err != null) return callback(err, null);
          // if full defined just return all the indexes directly
          if(full) return callback(null, indexes);
          // Return all the indexes
          callback(null, processResults(indexes));
        });
      }
    
      // Attempt to execute the listIndexes command
      self.command({listIndexes: name}, function(err, result) {
        if(err) return fallbackListIndexes();
        // if full defined just return all the indexes directly
        if(full) return callback(null, result.indexes);
        // Return all the indexes
        callback(null, processResults(result.indexes));
      });
    };

Reply all
Reply to author
Forward
0 new messages