2016-04-27 3 views
1

私の明示的なアプリケーション私は1つのレコードだけを見つけたらmLabデータベースを使用していますが、データベースからユーザーのリストを見つけるときに、この。mLab:レコードの代わりにdbオブジェクトを取得するすべてのユーザーを見つける

Query 
let User = app.db.collection("User"); 
User.find((err, users) => { 
    if (err) { return err; } 
    if (users) { 
     return res.json({ users: users}); 
    } 
}); 

私は単一のレコードそのが一致基準

User.findOne({ username: 'abc123' }, (err, user) => { 
     if (err) { return next(err); } 
     if (!user) { 
     return next(null, false, { 
      message: "Incorrect username." 
     }); 
     } 

でレコードを返すを見つけるためにしようと、私はこの方法のようにMongoの接続を設定する必要があり

Readable { 
    pool: null, 
    server: null, 
    disconnectHandler: { s: { storedOps: [], storeOptions: [Object], topology: [Object] }, 
    length: [Getter] }, bson: {}, ns: 'hs-cloud.User', cmd:  { find: 'hs-cloud.User', 
    limit: 0, 
    skip: 0, 
    query: {}, 
    slaveOk: true, 
    readPreference: { preference: 'primary', tags: undefined, options: undefined } }, options:  { skip: 0, 
    limit: 0, 
    raw: undefined, 
    hint: null, 
    timeout: undefined, 
    slaveOk: true, 
    readPreference: { preference: 'primary', tags: undefined, options: undefined }, 
    db: 
    EventEmitter { 
    domain: null, 
    _events: {}, 
    _eventsCount: 0, 
    _maxListeners: undefined, 
    s: [Object], 
    serverConfig: [Getter], 
    bufferMaxEntries: [Getter], 
    databaseName: [Getter] }, 
    promiseLibrary: [Function: Promise], 
    disconnectHandler: { s: [Object], length: [Getter] } }, topology:  EventEmitter { 
    domain: null, 
    _events: 
    { reconnect: [Function], 
    timeout: [Object], 
    error: [Object], 
    close: [Function], 
    destroy: [Object], 
    serverDescriptionChanged: [Function], 
    serverHeartbeatStarted: [Function], 
    serverHeartbeatSucceeded: [Function], 
    serverHearbeatFailed: [Function], 
    serverOpening: [Function], 
    serverClosed: [Function], 
    topologyOpening: [Function], 
    topologyClosed: [Function], 
    topologyDescriptionChanged: [Function] }, 
    _eventsCount: 14, 
    _maxListeners: undefined, 
    s: 
    { options: [Object], 
    callbacks: [Object], 
    logger: [Object], 
    state: 'connected', 
    reconnect: true, 
    reconnectTries: 30, 
    reconnectInterval: 1000, 
    emitError: true, 
    currentReconnectRetry: 30, 
    ismaster: [Object], 
    readPreferenceStrategies: undefined, 
    authProviders: [Object], 
    id: 3, 
    topologyId: -1, 
    tag: undefined, 
    disconnectHandler: [Object], 
    monitoring: false, 
    haInterval: 10000, 
    wireProtocolHandler: {}, 
    Cursor: [Object], 
    bsonInstance: {}, 
    inquireServerStateTimeout: null, 
    bson: {}, 
    pool: [Object], 
    isMasterLatencyMS: 138, 
    inTopology: false, 
    serverDetails: [Object], 
    serverDescription: null, 
    topologyDescription: null }, 
    hashedName: '1309e35791f04f1f4fc35d5e683e81d350dd04f4', 
    name: [Getter], 
    bson: [Getter], 
    wireProtocolHandler: [Getter], 
    id: [Getter] }, cursorState:  { cursorId: null, 
    cmd: 
    { find: 'hs-cloud.User', 
    limit: 0, 
    skip: 0, 
    query: {}, 
    slaveOk: true, 
    readPreference: [Object] }, 
    documents: [], 
    cursorIndex: 0, 
    dead: false, 
    killed: false, 
    init: false, 
    notified: false, 
    limit: 0, 
    skip: 0, 
    batchSize: 1000, 
    currentLimit: 0, 
    transforms: undefined }, callbacks: null, logger: { className: 'Cursor' }, _readableState:  ReadableState { 
    objectMode: true, 
    highWaterMark: 16, 
    buffer: [], 
    length: 0, 
    pipes: null, 
    pipesCount: 0, 
    flowing: null, 
    ended: false, 
    endEmitted: false, 
    reading: false, 
    sync: true, 
    needReadable: false, 
    emittedReadable: false, 
    readableListening: false, 
    defaultEncoding: 'utf8', 
    ranOut: false, 
    awaitDrain: 0, 
    readingMore: false, 
    decoder: null, 
    encoding: null }, readable: true, domain: null, _events: {},       _eventsCount: 0, _maxListeners: undefined, s:  { numberOfRetries: 5, 
    tailableRetryInterval: 500, 
    currentNumberOfRetries: 5, 
    state: 0, 
    streamOptions: {}, 
    bson: {}, 
    ns: 'hs-cloud.User', 
    cmd: 
    { find: 'hs-cloud.User', 
    limit: 0, 
    skip: 0, 
    query: {}, 
    slaveOk: true, 
    readPreference: [Object] }, 
    options: 
    { skip: 0, 
    limit: 0, 
    raw: undefined, 
    hint: null, 
    timeout: undefined, 
    slaveOk: true, 
    readPreference: [Object], 
    db: [Object], 
    promiseLibrary: [Function: Promise], 
    disconnectHandler: [Object] }, 
    topology: 
    EventEmitter { 
    domain: null, 
    _events: [Object], 
    _eventsCount: 14, 
    _maxListeners: undefined, 
    s: [Object], 
    hashedName: '1309e35791f04f1f4fc35d5e683e81d350dd04f4', 
    name: [Getter], 
    bson: [Getter], 
    wireProtocolHandler: [Getter], 
    id: [Getter] }, 
    topologyOptions: 
    { socketOptions: {}, 
    auto_reconnect: true, 
    host: 'ds030829.mlab.com', 
    port: 30829, 
    cursorFactory: [Object], 
    reconnect: true, 
    emitError: true, 
    size: 5, 
    disconnectHandler: [Object], 
    bson: {}, 
    messageHandler: [Function], 
    wireProtocolHandler: {} }, 
    promiseLibrary: [Function: Promise], 
    currentDoc: null }, timeout: false, sortValue: undefined 
} 

のような応答を取得しています

return new Promise((resolve, reject) => { 

MongoClient.connect('mongodb://<username>:<password>@ds033829.mlab.com:33829/my-cloud', (err, database) => { 
    let db = database; 
    if (err) { 
    return reject(err); 
    } 
    return resolve(db); 
}); 

});

と私は私の急行アプリ

よう
  • app.db = DBに格納しています。このデシベル。私は、クエリは同じですが、私はこのような何かを得ているもののそこMLABのドキュメントを参照してください私が欠けている

任意のアイデアの友人。

ありがとうございました。

答えて

0

findOne()はドキュメントを返すように見えますが、find()はカーソルを返しています。

var cursor = User.find(); 
    // Execute the each command, triggers for each document 
    cursor.forEach(function(doc) { 
     console.log(doc); 
    }, function(err) { 
     console.log(err); 
    });