2015-05-26 63 views
1

我是新來的node.js和mongo,我試圖用findOne()從我的數據庫的「測驗」集合中檢索一個對象,這樣我可以檢查其屬性。MongoDB findOne查詢不返回結果或undefined

我知道對象存在,因爲在蒙戈外殼,findOne()調用給了我這樣的:

> db.quizzes.findOne({ quiz_id:1 }) 
{ 
    "_id" : ObjectId("5564b0bf28b816e2462b6a1a"), 
    "quiz_id" : 1 
} 

在我的路線/ index.js,我有這樣的:

router.post('/submitanswer', function(req, res) { 
    var db = req.db; 
    var quizCollection = db.get('quizzes'); 
    var quiz = quizCollection.findOne({ quiz_id: 1 }); 
} 

console.log(quizCollection)給出:

{ manager: 
    { driver: 
     { _construct_args: [], 
     _native: [Object], 
     _emitter: [Object], 
     _state: 2, 
     _connect_args: [Object] }, 
    helper: { toObjectID: [Function], isObjectID: [Function], id: [Object] }, 
    collections: { quizzes: [Circular] }, 
    options: { safe: true }, 
    _events: {} }, 
    driver: 
    { _construct_args: [], 
    _native: 
     { domain: null, 
     _events: {}, 
     _maxListeners: undefined, 
     databaseName: 'quizzr', 
     serverConfig: [Object], 
     options: [Object], 
     _applicationClosed: false, 
     slaveOk: false, 
     bufferMaxEntries: -1, 
     native_parser: false, 
     bsonLib: [Object], 
     bson: [Object], 
     bson_deserializer: [Object], 
     bson_serializer: [Object], 
     _state: 'connected', 
     pkFactory: [Object], 
     forceServerObjectId: false, 
     safe: false, 
     notReplied: {}, 
     isInitializing: true, 
     openCalled: true, 
     commands: [], 
     logger: [Object], 
     tag: 1432673566484, 
     eventHandlers: [Object], 
     serializeFunctions: false, 
     raw: false, 
     recordQueryStats: false, 
     retryMiliSeconds: 1000, 
     numberOfRetries: 60, 
     readPreference: [Object] }, 
    _emitter: { domain: null, _events: {}, _maxListeners: 50 }, 
    _state: 2, 
    _connect_args: [ 'mongodb://localhost:27017/quizzr', [Object] ] }, 
    helper: 
    { toObjectID: [Function], 
    isObjectID: [Function], 
    id: 
     { [Function: ObjectID] 
     index: 847086, 
     createPk: [Function: createPk], 
     createFromTime: [Function: createFromTime], 
     createFromHexString: [Function: createFromHexString], 
     isValid: [Function: isValid], 
     ObjectID: [Circular], 
     ObjectId: [Circular] } }, 
    name: 'quizzes', 
    col: 
    { _construct_args: [], 
    _native: 
     { db: [Object], 
     collectionName: 'quizzes', 
     internalHint: null, 
     opts: {}, 
     slaveOk: false, 
     serializeFunctions: false, 
     raw: false, 
     readPreference: [Object], 
     pkFactory: [Object], 
     serverCapabilities: undefined }, 
    _emitter: { domain: null, _events: {}, _maxListeners: Infinity }, 
    _state: 2, 
    _skin_db: 
     { _construct_args: [], 
     _native: [Object], 
     _emitter: [Object], 
     _state: 2, 
     _connect_args: [Object] }, 
    _collection_args: [ 'quizzes', undefined ], 
    id: 
     { [Function: ObjectID] 
     index: 847086, 
     createPk: [Function: createPk], 
     createFromTime: [Function: createFromTime], 
     createFromHexString: [Function: createFromHexString], 
     isValid: [Function: isValid], 
     ObjectID: [Circular], 
     ObjectId: [Circular] }, 
    emitter: { domain: null, _events: {}, _maxListeners: Infinity } }, 
    options: {} } 

console.log(quiz)給出:

{ col: 
    { manager: 
     { driver: [Object], 
     helper: [Object], 
     collections: [Object], 
     options: [Object], 
     _events: {} }, 
    driver: 
     { _construct_args: [], 
     _native: [Object], 
     _emitter: [Object], 
     _state: 2, 
     _connect_args: [Object] }, 
    helper: { toObjectID: [Function], isObjectID: [Function], id: [Object] }, 
    name: 'quizzes', 
    col: 
     { _construct_args: [], 
     _native: [Object], 
     _emitter: [Object], 
     _state: 2, 
     _skin_db: [Object], 
     _collection_args: [Object], 
     id: [Object], 
     emitter: [Object] }, 
    options: {} }, 
    type: 'findOne', 
    opts: { quiz_id: 1, name: 1, fields: {}, safe: true }, 
    domain: null, 
    _events: { error: [Function], success: [Function] }, 
    _maxListeners: undefined, 
    emitted: {}, 
    ended: false, 
    success: [Function], 
    error: [Function], 
    complete: [Function], 
    resolve: [Function], 
    fulfill: [Function], 
    reject: [Function], 
    query: { quiz_id: 1 } } 

當然,試圖引用任何測驗屬性(例如, quiz('quiz_id'))未定義。

quizCollection.insert()似乎成功插入一個對象,所以我想我得到正確的集合。我認爲findOne()將返回未定義,如果它沒有找到任何東西,或者符合條件的對象,但我打印的東西似乎也不是。我如何檢索一個對象?

+0

您使用的是mongooseJS嗎? – staaar

+0

不,我不是 - 只是孟戈和和尚。 – bubbles

回答

1

NodeJS是異步的。一些API是同步的,但是findOne是異步的。

findOne有沒有返回值。您將在回調中獲得結果。大多數API返回一個錯誤作爲第一個參數,如果不是一個錯誤,將是不確定的,和您的查詢/ FS操作/淨操作等

示例的結果: quiz.findOne({quiz_id: 1}, function (err, quiz) {});

這個測試顯示如何查詢。 here

+0

啊,當然!非常感謝! – bubbles