Mongoose virtual fields included in toJSON by default: schemaOptions.toJSON.virtuals = true; still doesn't include virtual fields by default

旧巷老猫 提交于 2019-12-02 18:52:47

Just tried:

  var schemaOptions = {
    toObject: {
      virtuals: true
    }
  };

and worked! ;)

Now by default I use:

  var schemaOptions = {
    toObject: {
      virtuals: true
    }
    ,toJSON: {
      virtuals: true
    }
  };

You can do this way as well:

docs.set('toJSON', { virtuals: true });

For me it worked only after adding getters: true to schema options, as mentioned in mongoose docs, i.e.

var schemaOptions = { toObject: { getters: true }, toJSON: { getters: true } };

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!