Model.findOne 不返回文档而是返回一个包装对象 [英] Model.findOne not returning docs but returning a wrapper object

查看:21
本文介绍了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.

为什么会这样?

推荐答案

这正是 mongoose 的目的,包装 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天全站免登陆