Model.findOne不返回文档,但返回包装对象 [英] Model.findOne not returning docs but returning a wrapper object

查看:49
本文介绍了Model.findOne不返回文档,但返回包装对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这样的猫鼬定义了一个模型:

I have defined a Model with mongoose like this:

var mongoose = require("mongoose")
var Schema = mongoose.Schema

var userObject = Object.create({
    alias: String,
    email: String,
    password: String,
    updated: { 
        type: Date,
        default: Date.now
    }
})

var userSchema = new Schema(userObject, {strict: false})
var User = mongoose.model('User', userSchema)

module.exports = User

然后,我创建了一个可以通过mongo控制台完美找到的用户,如下所示:

Then I created a user that I can perfectly find through mongo console like this:

db.users.findOne({ email: "coco@coco.com" });
{
    "_id" : ObjectId("55e97420d82ebdea3497afc7"),
    "password" : "caff3a46ebe640e5b4175a26f11105bf7e18be76",
    "gravatar" : "a4bfba4352aeadf620acb1468337fa49",
    "email" : "coco@coco.com",
    "alias" : "coco",
    "updated" : ISODate("2015-09-04T10:36:16.059Z"),
    "apps" : [ ],
    "__v" : 0
}

但是,当我尝试通过带有mongoose的node.js访问此对象时,要检索的对象不是这样的文档,而是包装器:

However, when I try to access this object through a node.js with mongoose, the object a retrieve is not such doc, but a wrapper:

这段代码...

// Find the user for which the login queries
  var User = require('../models/User')
  User.findOne({ email: mail }, function(err, doc) {
    if (err) throw err
      if (doc) {
        console.dir(doc)
        if(doc.password == pass) // Passwords won't match

从console.dir(doc)...产生此输出...

Produces this output from console.dir(doc)...

{ '$__': 
   { strictMode: false,
     selected: undefined,
     shardval: undefined,
     saveError: undefined,
     validationError: undefined,
     adhocPaths: undefined,
     removing: undefined,
     inserting: undefined,
     version: undefined,
     getters: {},
     _id: undefined,
     populate: undefined,
     populated: undefined,
     wasPopulated: false,
     scope: undefined,
     activePaths: { paths: [Object], states: [Object], stateNames: [Object] },
     ownerDocument: undefined,
     fullPath: undefined,
     emitter: { domain: null, _events: {}, _maxListeners: 0 } },
  isNew: false,
  errors: undefined,
  _doc: 
   { __v: 0,
     apps: [],
     updated: Fri Sep 04 2015 12:36:16 GMT+0200 (CEST),
     alias: 'coco',
     email: 'coco@coco.com',
     gravatar: 'a4bfba4352aeadf620acb1468337fa49',
     password: 'caff3a46ebe640e5b4175a26f11105bf7e18be76',
     _id: { _bsontype: 'ObjectID', id: 'Uét Ø.½ê4¯Ç' } },
  '$__original_validate': { [Function] numAsyncPres: 0 },
  validate: [Function: wrappedPointCut],
  _pres: { '$__original_validate': [ [Object] ] },
  _posts: { '$__original_validate': [] } }

因此,密码不匹配,因为doc.password未定义.

Therefore, passwords won't match because doc.password is undefined.

这是为什么引起的?

推荐答案

这就是猫鼬的用途,它包装了mongo对象.它提供了在文档上调用猫鼬方法的功能.如果您希望使用简单的对象,则可以调用.toObject()或使用精简查询如果您根本不打算在其上使用任何猫鼬魔法.话虽如此,当doc.password返回doc._doc.password时,相等性检查仍应保持.

That's exactly the purpose of mongoose, wrapping mongo objects. It's what provides the ability to call mongoose methods on your documents. If you'd like the simple object, you can call .toObject() or use a lean query if you don't plan on using any mongoose magic on it at all. That being said, the equality check should still hold as doc.password returns doc._doc.password.

这篇关于Model.findOne不返回文档,但返回包装对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆