MongoDB findOne查询不返回结果或未定义

我是新来的node.js和mongo,我试图使用findOne()从我的数据库的“测验”集合检索一个对象,以便我可以检查其属性。

我知道这个对象是存在的,因为在Mongo shell中,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()将返回undefined,如果它没有find任何东西,或符合条件的对象,但我打印似乎并不是。 我如何检索一个对象?

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

findOne有没有返回值。 你会得到你的callback传递的结果。 大多数API会返回一个错误作为第一个参数,如果没有错误,那么这个错误是未定义的,以及查询/操作/networking操作的结果等。

例如: quiz.findOne({quiz_id: 1}, function (err, quiz) {});

此testing显示如何查询。 这里