select: false makes field values not accessible using "this.field_name"
See original GitHub issueWhen 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:
- Created 10 years ago
- Comments:10 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
you can override the select false fields with select for your queries (SO thread here)
My user schema in this case looked like:
@vkarpov15 Would it make sense to have access to a
this.select()
method to selectively request the missing fields only when necessary?For example:
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.