在从mongoose模型查询

时间:2018-03-25 17:55:53

标签: javascript json mongodb mongoose

对从mongoose模型查询返回的文档使用Object.assign()时,该对象返回我不想要的无关数据,并获取我需要使用doc._doc的实际文档数据 - 而不使用Object .assign()我可以立即访问文档数据(不在._doc中)。

UserModel.findOne(query, function(err, doc){

    //this outputs my document data just fine
    console.log(doc);

    //this outputs extraneous data, assigning someProp not to my document data,
    //but to the outer object.
    console.log(Object.assign({}, doc, { someProp: true});

})

以下是console.log(doc)的结果:

{ 
  _id: 5ab5df214869bc4bfc059fed,
  userID: 0,
  verified: false,
  verificationCode: 'vC_10a89847-f640-43fc-94cc-8ea532f5e05c',
  email: 'ee@ee.ee',
  createdAt: 2018-03-24T05:16:17.508Z,
  updatedAt: 2018-03-25T16:57:30.101Z,
  __v: 0
}

这是console.log的结果(Object.assign({},doc,{someProp:true});

{ '$__':
   InternalCache {
     strictMode: true,
     selected: {},
     shardval: undefined,
     saveError: undefined,
     validationError: undefined,
     adhocPaths: undefined,
     removing: undefined,
     inserting: undefined,
     version: undefined,
     getters: {},
     _id: 5ab5df214869bc4bfc059fed,
     populate: undefined,
     populated: undefined,
     wasPopulated: false,
     scope: undefined,
     activePaths: StateMachine { paths: [Object], states: [Object], stateNames: [Array] },
     pathsToScopes: {},
     ownerDocument: undefined,
     fullPath: undefined,
     emitter: EventEmitter { domain: null, _events: {}, _eventsCount: 0, _maxListeners: 0 },
     '$options': true },
  isNew: false,
  errors: undefined,
  _doc:
   { 
     _id: 5ab5df214869bc4bfc059fed,
     userID: 0,
     verified: false,
     verificationCode: 'vC_10a89847-f640-43fc-94cc-8ea532f5e05c',
     email: 'ee@ee.ee',
     createdAt: 2018-03-24T05:16:17.508Z,
     updatedAt: 2018-03-25T16:57:30.101Z,
     __v: 0 },
  '$init': true,
  someProp: true }

我的问题是,为什么我需要在使用doc = Object.assign({}, doc._doc, { someProp: true })内部和之后使用doc._doc来访问我的文档数据,但在使用._doc之前我可以在没有doc = Object.assign(...)的情况下访问数据属性}?

0 个答案:

没有答案