question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow hiding schema fields when calling toJSON

See original GitHub issue

This is a feature request to allow hiding of a schema field when converting the document into json. I’m not sure if it would be better if this was set from inside the schema declaration or from the toJSON call, e.g.

user = new Schema
  username: 
    type: String
  password:
    type: String
    expose: false

or something like

User.findOne({username: 'joe'}, function(err, doc) {
  user = doc.toJSON(hide: 'password')
});

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:53 (9 by maintainers)

github_iconTop GitHub Comments

9reactions
KuzminDimacommented, Mar 21, 2014
  UserSchema.method('toJSON', function() {
    var user = this.toObject();
    delete user.salt;
    delete user.hash;
    delete user.__v;
    return user;
  });
6reactions
olalondecommented, Oct 16, 2012

Has anyone solved this yet? Otherwise I’ll write that functionality when I have time. How about:

schema.set('toJSON', { hide: 'password salt' })

or maybe

var User = new Schema({
   password:                { type: String, toJSON: false }
   , salt:                  { type: String, toJSON: false }
});

There should probably be an option to ignore this, like:

doc.toJSON({showAll: true}); or doc.toJSON({ignoreOptions: true}); or doc.toRawJSON();

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hide properties of Mongoose objects in Node.JS JSON ...
The solution is to define a custom . toJSON() method on the Mongoose schema and delete the properties which you don't want to...
Read more >
How to protect the password field in Mongoose/MongoDB so it ...
I'm using for hiding password field in my REST JSON response UserSchema.methods.toJSON = function() { var obj = this.toObject(); //or var obj =...
Read more >
Mongoose v6.8.2: Schemas
Mongoose assigns each of your schemas an id virtual getter by default which returns the document's _id field cast to a string, or...
Read more >
Understanding JSON Schema
There are a number of online JSON Schema tools that allow you to run your own JSON schemas against example documents.
Read more >
OpenAPI Specification - Version 2.0 - Swagger
Models are described using the Schema Object which is a subset of JSON Schema ... This is global to all APIs but can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found