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.

select: false makes field values not accessible using "this.field_name"

See original GitHub issue

When using a plugin (I’m using mongoose-user) I’ve noticed that from methods defined in schema.methods inside a plugin (in my example mongoose-user.js) it is not possible to get the value of fields that have been marked as “select: false” in the model (in my example user.js)

So if you apply “select: false” to a field it won’t be accesible from a plugin. By removing this property or setting it to “select: true” the field is accessible again.

Updated info: I’ve also tested moving the plugin logic to user.js and the problem persists. I can’t read values using this.field from user.js methods too.

Sample using a plugin: (see line console.log('mongoose-use hashed_password', this.hashed_password); )

// model/user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var userPlugin = require('mongoose-user');

var UserSchema = new Schema({
  name: { type: String, default: ''  },
  email: { type: String, default: '' },
  hashed_password: { type: String, default: '', select: false },
});

UserSchema.plugin(userPlugin, {});

mongoose.model('User', UserSchema)

// mongoose-user.js

module.exports = userPlugin

function userPlugin (schema, options) {
var crypto = require('crypto');

  schema.methods.authenticate = function(plainText) {
    // THIS CONSOLE.LOG CAN'T ACCES TO this.hashed_password value
   // IT ALWAYS RETURN 'undefined'.    
    console.log('mongoose-use hashed_password', this.hashed_password);
    return this.encryptPassword(plainText) === this.hashed_password
  }

schema.methods.encryptPassword = function (password) {
    if (!password) return '';
    return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
   }

}

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

23reactions
connor11528commented, Aug 9, 2015

you can override the select false fields with select for your queries (SO thread here)

// add back the password field for this query
var query = User.findOne({
    email: req.body.email
}).select('_id email +password');

query.exec(function(err, user){
    // user with included password field

});

My user schema in this case looked like:

var userSchema = new Schema({
    email: {type: String, required: true, unique: true },
    password: {type: String, required: true, select: false },
    admin: Boolean
});
4reactions
kesarioncommented, Aug 11, 2016

@vkarpov15 Would it make sense to have access to a this.select() method to selectively request the missing fields only when necessary?

For example:

ResourceSchema
    .virtual('voted')
    .get(function() {
        if (global.user) {
            this.select( 'voters');
            return _.includes(this.voters, global.user._id);
        }
        return null;
    });

Voters would hypothetically contain the ids of users who have voted (some resource). Let’s say we don’t want to select them by default, but want to check if the logged in user has already voted (set in global in the example).

I suppose it would be less efficient, needing another query to the database, but in some cases where performance isn’t critical it might make sense to know that’s automatically taken care of.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set default values for fields or controls - Microsoft Support
Select the lookup field, and on the General tab, in the Default Value property box, type the key value that you noted in...
Read more >
Java reflection - access protected field - Stack Overflow
My class tree looks like this A->B->C and inside C I cannot get the value of a protected field declared in A. Note...
Read more >
How to Troubleshoot and Fix Excel Pivot Table Errors
How to troubleshoot and fix Excel pivot table errors, such as PivotTable field name is not valid. Find the problem, and fix it....
Read more >
Invalid field value: Invalid Code Table Entry [field name] Error
Possible causes: The Data type for the Attribute does not correspond with the Table entry; The value does not exist as a table...
Read more >
Salesforce field name not visible when mapping field in Pardot
If this Salesforce field is mapped to the incorrect Pardot field or the user would like to remap it, edit the field in...
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