2016-11-30 124 views
1

我attemping在我的数据库运行mongoose.findOne但我得到意想不到的results.My查询Mongoose.js findOne返回查询的元数据

const User = mongoose.model('User', {name: String, email: String, passwordHash: String, validation: String, validationCode: String, favorites: Array }) 


exports.findUser = function findUser(email){ 

    const foundUser = User.findOne({email: email}, function(err, userObj){ 
     if(err){ 
      return err 
     }  else if (userObj){ 
      return userObj 
     }  else{ 
      return null 
     } 
    }) 

    return foundUser 
} 

然而,这返回下面的数据(看似随机的?)而现在没有我不知道如何解决这个问题,我要求

Query { 
    _mongooseOptions: {}, 
    mongooseCollection: 
    NativeCollection { 
    collection: null, 
    opts: { bufferCommands: true, capped: false }, 
    name: 'users', 
    collectionName: 'users', 
    conn: 
     NativeConnection { 
     base: [Object], 
     collections: [Object], 
     models: [Object], 
     config: [Object], 
     replica: false, 
     hosts: null, 
     host: 'ds113938.mlab.com', 
     port: 13938, 
     user: 'root', 
     pass: 'root', 
     name: 'users', 
     options: [Object], 
     otherDbs: [], 
     _readyState: 2, 
     _closeCalled: false, 
     _hasOpened: false, 
     _listening: false, 
     db: [Object] }, 
    queue: [], 
    buffer: true, 
    emitter: 
     EventEmitter { 
     domain: null, 
     _events: {}, 
     _eventsCount: 0, 
     _maxListeners: undefined } }, 
    model: 
    { [Function: model] 
    hooks: Kareem { _pres: {}, _posts: {} }, 
    base: 
     Mongoose { 
     connections: [Object], 
     plugins: [], 
     models: [Object], 
     modelSchemas: [Object], 
     options: [Object] }, 
    modelName: 'User', 
    model: [Function: model], 
    db: 
     NativeConnection { 
     base: [Object], 
     collections: [Object], 
     models: [Object], 
     config: [Object], 
     replica: false, 
     hosts: null, 
     host: 'ds113938.mlab.com', 
     port: 13938, 
     user: 'root', 
     pass: 'root', 
     name: 'users', 
     options: [Object], 
     otherDbs: [], 
     _readyState: 2, 
     _closeCalled: false, 
     _hasOpened: false, 
     _listening: false, 
     db: [Object] }, 
    discriminators: undefined, 
    schema: 
     Schema { 
     obj: [Object], 
     paths: [Object], 
     subpaths: {}, 
     virtuals: [Object], 
     singleNestedPaths: {}, 
     nested: {}, 
     inherits: {}, 
     callQueue: [Object], 
     _indexes: [], 
     methods: {}, 
     statics: {}, 
     tree: [Object], 
     _requiredpaths: undefined, 
     discriminatorMapping: undefined, 
     _indexedpaths: undefined, 
     query: {}, 
     childSchemas: [], 
     s: [Object], 
     options: [Object], 
     '$globalPluginsApplied': true }, 
    collection: 
     NativeCollection { 
     collection: null, 
     opts: [Object], 
     name: 'users', 
     collectionName: 'users', 
     conn: [Object], 
     queue: [], 
     buffer: true, 
     emitter: [Object] }, 
    Query: { [Function] base: [Object] }, 
    '$__insertMany': [Function], 
    insertMany: [Function] }, 
    schema: 
    Schema { 
    obj: 
     { name: [Function: String], 
     email: [Function: String], 
     passwordHash: [Function: String], 
     validation: [Function: String], 
     validationCode: [Function: String], 
     favorites: [Function: Array] }, 
    paths: 
     { name: [Object], 
     email: [Object], 
     passwordHash: [Object], 
     validation: [Object], 
     validationCode: [Object], 
     favorites: [Object], 
     _id: [Object], 
     __v: [Object] }, 
    subpaths: {}, 
    virtuals: { id: [Object] }, 
    singleNestedPaths: {}, 
    nested: {}, 
    inherits: {}, 
    callQueue: [ [Object], [Object], [Object], [Object] ], 
    _indexes: [], 
    methods: {}, 
    statics: {}, 
    tree: 
     { name: [Function: String], 
     email: [Function: String], 
     passwordHash: [Function: String], 
     validation: [Function: String], 
     validationCode: [Function: String], 
     favorites: [Function: Array], 
     _id: [Object], 
     id: [Object], 
     __v: [Function: Number] }, 
    _requiredpaths: undefined, 
    discriminatorMapping: undefined, 
    _indexedpaths: undefined, 
    query: {}, 
    childSchemas: [], 
    s: { hooks: [Object], kareemHooks: [Object] }, 
    options: 
     { retainKeyOrder: false, 
     typeKey: 'type', 
     id: true, 
     noVirtualId: false, 
     _id: true, 
     noId: false, 
     validateBeforeSave: true, 
     read: null, 
     shardKey: null, 
     autoIndex: null, 
     minimize: true, 
     discriminatorKey: '__t', 
     versionKey: '__v', 
     capped: false, 
     bufferCommands: true, 
     strict: true, 
     pluralization: true }, 
    '$globalPluginsApplied': true }, 
    op: 'findOne', 
    options: { retainKeyOrder: false }, 
    _conditions: { email: '[email protected]' }, 
    _fields: undefined, 
    _update: undefined, 
    _path: undefined, 
    _distinct: undefined, 
    _collection: 
    NodeCollection { 
    collection: 
     NativeCollection { 
     collection: null, 
     opts: [Object], 
     name: 'users', 
     collectionName: 'users', 
     conn: [Object], 
     queue: [], 
     buffer: true, 
     emitter: [Object] }, 
    collectionName: 'users' }, 
    _traceFunction: undefined, 
    _castError: null, 
    _count: [Function], 
    _execUpdate: [Function], 
    _find: [Function], 
    _findOne: [Function], 
    _findOneAndRemove: [Function], 
    _findOneAndUpdate: [Function] } 

数据,这似乎是我试图运行查询的概述和不结果表示查询

回答

1

试试这个:

const User = mongoose.model('User', {name: String, email: String, passwordHash: String, validation: String, validationCode: String, favorites: Array }) 


exports.findUser = function findUser(email, callback){ 
    User.findOne({email: email}, function(err, userObj){ 
     if(err){ 
      return callback(err); 
     } else if (userObj){ 
      return callback(null,userObj); 
     } else { 
      return callback(); 
     } 
    }); 
} 

当您执行User.findOne,猫鼬调用的MongoDB和回调(在你的findOne功能最后PARAM)回报您的用户,这样可以返回找到用户调用回调。

要调用findUser功能,你需要传递一个回调,这样的事情:

findUser('[email protected]', function(error, userFound) { 
    console.log(userFound); 
}); 

你可以找到更多详细的猫鼬findOne here并了解回调函数,你可以去看看here

+0

该死的9秒快:d – xShirase

+0

哈哈哈,比赛条件:P – danilodeveloper

+0

@xhihirase,最有趣的是我们的答案非常相似。哈哈 – danilodeveloper

0

你所看到的是一个Mongoose查询对象return foundUser的结果。

你混合同步和异步代码,你必须等待被执行您的查询,回调调用它的功能,像这样:

const User = mongoose.model('User', {name: String, email: String, passwordHash: String, validation: String, validationCode: String, favorites: Array }); 

exports.findUser = function findUser(email,callback){ 
    const foundUser = User.findOne({email: email}, (err, userObj)=>{ 
     if(err){ 
      callback(err) 
     } else if (userObj){ 
      callback(null,userObj) 
     } else { 
      callback(new Error('Some strange thing has happened)); 
     } 
    }); 
} 

你再打电话给你的函数是这样的:

findUser((err,user)=>{ 
    if(err) console.log(err); 
    console.log(user) 
}); 

对于一个快速介绍到回调,这是链接到开始What is a callback function?