Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8551301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:10:00+00:00 2026-06-11T14:10:00+00:00

the scenarios are as follows scenario #1 someSchema.pre(‘save’, function(next){ asyncFunction(function(){ this.sub.value = ‘something’ next()

  • 0

the scenarios are as follows

scenario #1

someSchema.pre('save', function(next){
  asyncFunction(function(){
    this.sub.value = 'something'
    next()
  })
})

This fails because this changes context and now represents asyncFunction, and so I can’t modify the incoming data within a function. It comes up with an error that this.sub is not defined

scenario #2, based on the information from hooks-js

some.Schema.pre('save', function(next){
  asyncFunction(function(){
    next('something')
  });
});
some.Schema.pre('save', function(next, value){
  this.sub.value=value
  next()
})

This works, in so far as that it modifies the values, and the mongodb side is fine, however it just hangs, and never continues after saving the document.

Am I doing something wrong? is there a better way of doing this? Or is this a bug

scenario #3 originally failed but now seems to work

someSchema.pre('save', function(next){
  x = this
  asyncFunction(function(){
    x.sub.value = 'something'
    next()
  })
})

That said, I am still curious as to why scenario #2 didn’t work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:10:02+00:00Added an answer on June 11, 2026 at 2:10 pm

    In the first scenario you need to capture the original this context you want to make available to the callback like this:

    someSchema.pre('save', function(next){
      var self = this;
      asyncFunction(function(){
        self.sub.value = 'something'
        next()
      })
    })
    

    In the second scenario I think you’re a bit off in the weeds. You can’t pass values from one middleware function to next via parameters. When you use the two parameter callback version of the middleware you’re marking it as parallel middleware and the second parameter is the done function that must be called when the callback is done with its processing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The architecture for this scenario is as follows: I have a table of items
I am having difficulties with listing this type of data. Scenario is as follows:
My scenario is as follows - I have a client C with function foo()
My scenario is as follows:(C++) In char a[10], the array a[] has elements (numbers)
The scenario is as follows. My Order model has an after_create that contacts a
The scenario is as follows, I have 3 objects (i simplified the names) named
My scenario is as follows. I have 7 tabs (one for each day) using
The scenario I am using LINQ is as follows: I have a method that
I used to create scenarios where in scenario name I explain what is scenario.
I have an sql scenario as follows which I have been trying to improve.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.