无法从Mongo / Node中的cursor.toArray()获取响应

时间:2014-01-23 07:46:58

标签: node.js node-mongodb-native fedora16

节点0.10.24 + 32位linux上的mongo节点驱动程序1.3.23

我的回调永远不会被执行。

  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);
        }

      });
    }
  });

本节的输出如下:

{ 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

看起来集合的第一个console.log()没有完成。但其他痕迹出现了。为什么不执行回调?为什么我看不到“cursor.toArray()返回”?

当我搬到新服务器时,这停止了工作。旧服务器正在运行节点0.10.13和mongodb 1.2.13。

2 个答案:

答案 0 :(得分:0)

来自record_collection追踪:

db.serverConfig.connected = false;

数据库未连接。

答案 1 :(得分:-1)

不使用.toArray,而是使用.exec,即

cursor.exec(function(error, docs){
// code here
});

那应该可以解决问题。