Mongoose pre/post midleware can't access [this] instance using ES6
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have dilemma, trying to add some pre-logic to a mongoose model using pre
middleware and can not access the this
instance as usual.
UserSchema.pre('save', next => {
console.log(this); // logs out empty object {}
let hash = crypto.createHash('sha256');
let password = this.password;
console.log("Hashing password, " + password);
hash.update(password);
this.password = hash.digest('hex');
next();
});
Question: *Is there a way to access the this
instance?
node.js mongoose
add a comment |
I have dilemma, trying to add some pre-logic to a mongoose model using pre
middleware and can not access the this
instance as usual.
UserSchema.pre('save', next => {
console.log(this); // logs out empty object {}
let hash = crypto.createHash('sha256');
let password = this.password;
console.log("Hashing password, " + password);
hash.update(password);
this.password = hash.digest('hex');
next();
});
Question: *Is there a way to access the this
instance?
node.js mongoose
1
He is in trouble similar your situation. As the maintainer said, retaining context is a feature of arrow notation, you should usefunction () {}
notation and it will give you (and your co-worker) the meaning that it doesn't work with retaining context using arrow notation.
– Jinyoung Kim
Apr 30 '16 at 17:03
add a comment |
I have dilemma, trying to add some pre-logic to a mongoose model using pre
middleware and can not access the this
instance as usual.
UserSchema.pre('save', next => {
console.log(this); // logs out empty object {}
let hash = crypto.createHash('sha256');
let password = this.password;
console.log("Hashing password, " + password);
hash.update(password);
this.password = hash.digest('hex');
next();
});
Question: *Is there a way to access the this
instance?
node.js mongoose
I have dilemma, trying to add some pre-logic to a mongoose model using pre
middleware and can not access the this
instance as usual.
UserSchema.pre('save', next => {
console.log(this); // logs out empty object {}
let hash = crypto.createHash('sha256');
let password = this.password;
console.log("Hashing password, " + password);
hash.update(password);
this.password = hash.digest('hex');
next();
});
Question: *Is there a way to access the this
instance?
node.js mongoose
node.js mongoose
edited Jul 20 '17 at 15:23
Cœur
19.3k9116155
19.3k9116155
asked Apr 30 '16 at 16:35
Alexandru OlaruAlexandru Olaru
3,51821542
3,51821542
1
He is in trouble similar your situation. As the maintainer said, retaining context is a feature of arrow notation, you should usefunction () {}
notation and it will give you (and your co-worker) the meaning that it doesn't work with retaining context using arrow notation.
– Jinyoung Kim
Apr 30 '16 at 17:03
add a comment |
1
He is in trouble similar your situation. As the maintainer said, retaining context is a feature of arrow notation, you should usefunction () {}
notation and it will give you (and your co-worker) the meaning that it doesn't work with retaining context using arrow notation.
– Jinyoung Kim
Apr 30 '16 at 17:03
1
1
He is in trouble similar your situation. As the maintainer said, retaining context is a feature of arrow notation, you should use
function () {}
notation and it will give you (and your co-worker) the meaning that it doesn't work with retaining context using arrow notation.– Jinyoung Kim
Apr 30 '16 at 17:03
He is in trouble similar your situation. As the maintainer said, retaining context is a feature of arrow notation, you should use
function () {}
notation and it will give you (and your co-worker) the meaning that it doesn't work with retaining context using arrow notation.– Jinyoung Kim
Apr 30 '16 at 17:03
add a comment |
1 Answer
1
active
oldest
votes
The fat arrow notation (=>
) is not useful in this situation. Instead, just use the old fashioned anonymous function notation:
UserSchema.pre('save', function(next) {
...
});
The reason is that the fat arrow lexically binds the function to the current scope (more on that here, but TL;DR: the fat arrow notation is not meant to be a generic shortcut notation, it's meant specifically to create lexically bound functions), whereas the function should be called in a scope provided by Mongoose.
2
That is correct, but you might want to add a reference to arrow functions, potentially quoting the reason why they cannot be used here. ;)
– E_net4
Apr 30 '16 at 17:02
@E_net4 fair enough, I'll update my answer =D
– robertklep
Apr 30 '16 at 17:03
1
@AlexandruOlaru there's still a reason for the old notation to exist, so even though the fat arrow notation may look better, it's not meant to be used in this particular case.
– robertklep
Apr 30 '16 at 17:08
1
@AlexandruOlaru That issue simply derives from using the wrong construct. Just don't use arrow functions there, or any other case where you need to use the boundthis
.
– E_net4
Apr 30 '16 at 17:09
2
Thx guys, I learned something new today about=>
– Alexandru Olaru
Apr 30 '16 at 17:10
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36957440%2fmongoose-pre-post-midleware-cant-access-this-instance-using-es6%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The fat arrow notation (=>
) is not useful in this situation. Instead, just use the old fashioned anonymous function notation:
UserSchema.pre('save', function(next) {
...
});
The reason is that the fat arrow lexically binds the function to the current scope (more on that here, but TL;DR: the fat arrow notation is not meant to be a generic shortcut notation, it's meant specifically to create lexically bound functions), whereas the function should be called in a scope provided by Mongoose.
2
That is correct, but you might want to add a reference to arrow functions, potentially quoting the reason why they cannot be used here. ;)
– E_net4
Apr 30 '16 at 17:02
@E_net4 fair enough, I'll update my answer =D
– robertklep
Apr 30 '16 at 17:03
1
@AlexandruOlaru there's still a reason for the old notation to exist, so even though the fat arrow notation may look better, it's not meant to be used in this particular case.
– robertklep
Apr 30 '16 at 17:08
1
@AlexandruOlaru That issue simply derives from using the wrong construct. Just don't use arrow functions there, or any other case where you need to use the boundthis
.
– E_net4
Apr 30 '16 at 17:09
2
Thx guys, I learned something new today about=>
– Alexandru Olaru
Apr 30 '16 at 17:10
|
show 3 more comments
The fat arrow notation (=>
) is not useful in this situation. Instead, just use the old fashioned anonymous function notation:
UserSchema.pre('save', function(next) {
...
});
The reason is that the fat arrow lexically binds the function to the current scope (more on that here, but TL;DR: the fat arrow notation is not meant to be a generic shortcut notation, it's meant specifically to create lexically bound functions), whereas the function should be called in a scope provided by Mongoose.
2
That is correct, but you might want to add a reference to arrow functions, potentially quoting the reason why they cannot be used here. ;)
– E_net4
Apr 30 '16 at 17:02
@E_net4 fair enough, I'll update my answer =D
– robertklep
Apr 30 '16 at 17:03
1
@AlexandruOlaru there's still a reason for the old notation to exist, so even though the fat arrow notation may look better, it's not meant to be used in this particular case.
– robertklep
Apr 30 '16 at 17:08
1
@AlexandruOlaru That issue simply derives from using the wrong construct. Just don't use arrow functions there, or any other case where you need to use the boundthis
.
– E_net4
Apr 30 '16 at 17:09
2
Thx guys, I learned something new today about=>
– Alexandru Olaru
Apr 30 '16 at 17:10
|
show 3 more comments
The fat arrow notation (=>
) is not useful in this situation. Instead, just use the old fashioned anonymous function notation:
UserSchema.pre('save', function(next) {
...
});
The reason is that the fat arrow lexically binds the function to the current scope (more on that here, but TL;DR: the fat arrow notation is not meant to be a generic shortcut notation, it's meant specifically to create lexically bound functions), whereas the function should be called in a scope provided by Mongoose.
The fat arrow notation (=>
) is not useful in this situation. Instead, just use the old fashioned anonymous function notation:
UserSchema.pre('save', function(next) {
...
});
The reason is that the fat arrow lexically binds the function to the current scope (more on that here, but TL;DR: the fat arrow notation is not meant to be a generic shortcut notation, it's meant specifically to create lexically bound functions), whereas the function should be called in a scope provided by Mongoose.
edited Apr 30 '16 at 17:06
answered Apr 30 '16 at 16:50
robertkleprobertklep
140k19237251
140k19237251
2
That is correct, but you might want to add a reference to arrow functions, potentially quoting the reason why they cannot be used here. ;)
– E_net4
Apr 30 '16 at 17:02
@E_net4 fair enough, I'll update my answer =D
– robertklep
Apr 30 '16 at 17:03
1
@AlexandruOlaru there's still a reason for the old notation to exist, so even though the fat arrow notation may look better, it's not meant to be used in this particular case.
– robertklep
Apr 30 '16 at 17:08
1
@AlexandruOlaru That issue simply derives from using the wrong construct. Just don't use arrow functions there, or any other case where you need to use the boundthis
.
– E_net4
Apr 30 '16 at 17:09
2
Thx guys, I learned something new today about=>
– Alexandru Olaru
Apr 30 '16 at 17:10
|
show 3 more comments
2
That is correct, but you might want to add a reference to arrow functions, potentially quoting the reason why they cannot be used here. ;)
– E_net4
Apr 30 '16 at 17:02
@E_net4 fair enough, I'll update my answer =D
– robertklep
Apr 30 '16 at 17:03
1
@AlexandruOlaru there's still a reason for the old notation to exist, so even though the fat arrow notation may look better, it's not meant to be used in this particular case.
– robertklep
Apr 30 '16 at 17:08
1
@AlexandruOlaru That issue simply derives from using the wrong construct. Just don't use arrow functions there, or any other case where you need to use the boundthis
.
– E_net4
Apr 30 '16 at 17:09
2
Thx guys, I learned something new today about=>
– Alexandru Olaru
Apr 30 '16 at 17:10
2
2
That is correct, but you might want to add a reference to arrow functions, potentially quoting the reason why they cannot be used here. ;)
– E_net4
Apr 30 '16 at 17:02
That is correct, but you might want to add a reference to arrow functions, potentially quoting the reason why they cannot be used here. ;)
– E_net4
Apr 30 '16 at 17:02
@E_net4 fair enough, I'll update my answer =D
– robertklep
Apr 30 '16 at 17:03
@E_net4 fair enough, I'll update my answer =D
– robertklep
Apr 30 '16 at 17:03
1
1
@AlexandruOlaru there's still a reason for the old notation to exist, so even though the fat arrow notation may look better, it's not meant to be used in this particular case.
– robertklep
Apr 30 '16 at 17:08
@AlexandruOlaru there's still a reason for the old notation to exist, so even though the fat arrow notation may look better, it's not meant to be used in this particular case.
– robertklep
Apr 30 '16 at 17:08
1
1
@AlexandruOlaru That issue simply derives from using the wrong construct. Just don't use arrow functions there, or any other case where you need to use the bound
this
.– E_net4
Apr 30 '16 at 17:09
@AlexandruOlaru That issue simply derives from using the wrong construct. Just don't use arrow functions there, or any other case where you need to use the bound
this
.– E_net4
Apr 30 '16 at 17:09
2
2
Thx guys, I learned something new today about
=>
– Alexandru Olaru
Apr 30 '16 at 17:10
Thx guys, I learned something new today about
=>
– Alexandru Olaru
Apr 30 '16 at 17:10
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36957440%2fmongoose-pre-post-midleware-cant-access-this-instance-using-es6%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
He is in trouble similar your situation. As the maintainer said, retaining context is a feature of arrow notation, you should use
function () {}
notation and it will give you (and your co-worker) the meaning that it doesn't work with retaining context using arrow notation.– Jinyoung Kim
Apr 30 '16 at 17:03