为什么我在控制台打印mongoose的模型对象时会看到额外的字段,我正在使用node JS和mongoose,我如何隐藏这些属性

ma8fv8wu  于 5个月前  发布在  Go
关注(0)|答案(3)|浏览(57)
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/playgroundDB')
.then(()=>console.log('Connected to Mongo DB....'))
.catch(( err)=>console.log('Could not connect to MongoDb', err));

const courseSchema = new mongoose.Schema({
    name : String,
    author : String,
    tags :[String],
    date:{ type: Date, default:Date.now},
    isPublished: Boolean
});

const Course= mongoose.model('Course', courseSchema);

const course = new Course({
    name : "xxxx",
    author : 'rahul123',
    tags :['Frontend', 'Backend'] ,
    isPublished : true
});

course.save().then(res=>console.log(res)).catch(err=> console.log(err));

字符串
当我执行上面的代码时,我的数据被保存在db中,当我使用(res=>console.log(res))打印它时,我在控制台中看到了额外的字段。

下面是我在控制台的输出

model {
      '$__': InternalCache {
        strictMode: true,
        selected: undefined,
        shardval: undefined,
        saveError: undefined,
        validationError: undefined,
        adhocPaths: undefined,
        removing: undefined,
        inserting: true,
        version: undefined,
        getters: {},
        _id: 5dea6a7256bf9212c81361a9,
        populate: undefined,
        populated: undefined,
        wasPopulated: false,
        scope: undefined,
        activePaths: StateMachine {
          paths: {},
          states: [Object],
          stateNames: [Array],
          forEach: [Function],
          map: [Function]
        },
        pathsToScopes: {},
        ownerDocument: undefined,
        fullPath: undefined,
        emitter: EventEmitter {
          _events: [Object: null prototype] {},
          _eventsCount: 0,
          _maxListeners: 0
        },
        '$options': true
      },
      isNew: false,
      errors: undefined,
      _doc: {
        tags: [
          'Frontend',
          'Backend',
          toBSON: [Function: toBSON],
          _atomics: {},
          _parent: [Circular],
          _cast: [Function: _cast],
          _markModified: [Function: _markModified],
          _registerAtomic: [Function: _registerAtomic],
          '$__getAtomics': [Function: $__getAtomics],
          hasAtomics: [Function: hasAtomics],
          _mapCast: [Function: _mapCast],
          push: [Function: push],
          nonAtomicPush: [Function: nonAtomicPush],
          '$pop': [Function: $pop],
          pop: [Function: pop],
          '$shift': [Function: $shift],
          shift: [Function: shift],
          pull: [Function: pull],
          splice: [Function: splice],
          unshift: [Function: unshift],
          sort: [Function: sort],
          addToSet: [Function: addToSet],
          set: [Function: set],
          toObject: [Function: toObject],
          inspect: [Function: inspect],
          indexOf: [Function: indexOf],
          remove: [Function: pull],
          _path: 'tags',
          isMongooseArray: true,
          validators: [],
          _schema: [SchemaArray]
        ],
        date: 2019-12-06T14:49:22.524Z,
        _id: 5dea6a7256bf9212c81361a9,
        name: 'xxxx',
        author: 'rahul123',
        isPublished: true,
        __v: 0
      }
    }


正如你在最后一节中看到的,我在最后一节中看到了我保存的数据。但是为什么我看到了这些额外的属性。我已经用Mongo DB version 3.6.164.2测试了相同的代码。
我怎样才能摆脱这些田地。

brvekthn

brvekthn1#

我也遇到了同样的问题。解决这个问题的简单方法是在控制台语句中添加toObject(),如下所示:

(res=>console.log(res.toObject()))

字符串
这将删除多余的字段。

5m1hhzi4

5m1hhzi42#

试试看:

console.log(JSON.stringify(res));

字符串

jvlzgdj9

jvlzgdj93#

为了解决这个问题,我创建了一个函数

const cloneObj= (data)=>{
  let po = JSON.stringify(data)
  return JSON.parse(po)
}

字符串
并将我的数据通过该函数进行过滤

let data = cloneObj(userData)


现在它的工作正常:)

相关问题