Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Trouble getting a response from cursor.toArray() in Mongo/Node

Trouble getting a response from cursor.toArray() in Mongo/Node

Problem

node 0.10.24 + mongo node driver 1.3.23 on 32 bit linux

My callback here is never getting executed.

  console.log(record_collection);
  record_collection.find({}, function (error, cursor) {

    console.log("record_collection.find() returned");

    if (error) {
      console.log(error);
      callback(error);
    }
    else {

      console.log("calling toArray with cursor");

      cursor.toArray(function(error, docs){

        console.log("cursor.toArray() returned");

        if (error) {
          console.log(error);
          callback(error);
        }
        else {
          callback(null, docs);
        }

      });
    }
  });

The output from this section goes like this:

{ db: 
   { domain: null,
     _events: {},
     _maxListeners: 10,
     databaseName: 'dbname',
     serverConfig: 
      { domain: null,
        _events: {},
        _maxListeners: 10,
        _callBackStore: [Object],
        _commandsStore: [Object],
        auth: [Object],
        _dbStore: [Object],
        host: 'localhost',
        port: 27017,
        options: [Object],
        internalMaster: false,
        connected: false,
        poolSize: 5,
        disableDriverBSONSizeCheck: false,
        _used: true,
        replicasetInstance: null,
        emitOpen: true,
        ssl: false,
        sslValidate: false,
        sslCA: null,
        sslCert: undefined,
        sslKey: undefined,
        sslPass: undefined,
        serverCapabilities: null,
        name: 'localhost:27017',
        _readPreference: null,
        socketOptions: [Object],
        logger: [Object],
        eventHandlers: [Object],
        _serverState: 'connecting',
        _state: [Object],
        recordQueryStats: false,
        socketTimeoutMS: [Getter/Setter],
        db: [Circular],
        dbInstances: [Object],
        connectionPool: [Object] },
     options: {},
     _applicationClosed: false,
     slaveOk: false,
     bufferMaxEntries: -1,
     native_parser: undefined,
     bsonLib: 
      { Code: [Function: Code],
        Symbol: [Function: Symbol],
        BSON: [Object],
        DBRef: [Function: DBRef],
        Binary: [Object],
        ObjectID: [Object],
        Long: [Object],
        Timestamp: [Object],
        Double: [Function: Double],
        MinKey: [Function: MinKey],
        MaxKey: [Function: MaxKey],
        promoteLongs: true },
     bson: { promoteLongs: true },
     bson_deserializer: 
      { Code: [Function: Code],
        Symbol: [Function: Symbol],
        BSON: [Object],
        DBRef: [Function: DBRef],
        Binary: [Object],
        ObjectID: [Object],
        Long: [Object],
        Timestamp: [Object],
        Double: [Function: Double],
        MinKey: [Function: MinKey],
        MaxKey: [Function: MaxKey],
        promoteLongs: true },
     bson_serializer: 
      { Code: [Function: Code],
        Symbol: [Function: Symbol],
        BSON: [Object],
        DBRef: [Function: DBRef],
        Binary: [Object],
        ObjectID: [Object],
        Long: [Object],
        Timestamp: [Object],
        Double: [Function: Double],
        MinKey: [Function: MinKey],
        MaxKey: [Function: MaxKey],
        promoteLongs: true },
     _state: 'connecting',
     pkFactory: 
      { [Function: ObjectID]
        index: 14382708,
        createPk: [Function: createPk],
        createFromTime: [Function: createFromTime],
        createFromHexString: [Function: createFromHexString] },
     forceServerObjectId: false,
     safe: false,
     notReplied: {},
     isInitializing: true,
     openCalled: true,
     commands: [],
     logger: { error: [Function], log: [Function], debug: [Function] },
     tag: 1390462362041,
     eventHandlers: 
      { error: [],
        parseError: [],
        poolReady: [],
        message: [],
        close: [] },
     serializeFunctions: false,
     raw: false,
     recordQueryStats: false,
     retryMiliSeconds: 1000,
     numberOfRetries: 60,
     readPreference: undefined },
  collectionName: 'gis_stops',
  internalHint: null,
  opts: {},
  slaveOk: false,
  serializeFunctions: false,
  raw: false,
  readPreference: 'primary',
  pkFactory: 
   { [Function: ObjectID]
     index: 14382708,
     createPk: [Function: createPk],
     createFromTime: [Function: createFromTime],
     createFromHexString: [Function: createFromHexString] },
  serverCapabilities: undefined }
record_collection.find() returned
calling toArray with cursor

It looks like the first console.log() of the collection doesn't finish. But the other traces appear. Why isn't the callback being executed? Why don't I see "cursor.toArray() returned"?

This stopped working when I moved to a new server. The old server was running node 0.10.13 and mongodb 1.2.13.

Problem courtesy of: Joe Beuckman

Solution

From the record_collection trace:

db.serverConfig.connected = false;

The database was not connected.

Solution courtesy of: Joe Beuckman

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Trouble getting a response from cursor.toArray() in Mongo/Node

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×