Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After hook in service not running when same endpoint called in quick succession #308

Closed
ivanELEC opened this issue Jul 18, 2019 · 2 comments

Comments

@ivanELEC
Copy link

ivanELEC commented Jul 18, 2019

Steps to reproduce

I have a service call that has a before and after hook.

The before gets some associated data (the data that is associated depends on a query parameter)

The after hook does some data processing to format the data for a front-end, the changes in the data are overwritten in the context.result

Before Hook

get: [
       async function (context) {
          log.info("calling: " + rootServiceName + "get - before");
          //Call for basic learning pathway information
          if (includeValue) {
            //only select user information if getting basic user info
            if(includeValue==1){
              //select only needed fields
              context.params.query.$select = ['id','firstName',"lastName","email","avatar","user_pathways"];
            }else if(includeValue==2){
              //don't select any user fields (not needed)
              context.params.query.$select = ["user_pathways"]
            }else{
             // context.params.query.$select = ["id"];
            }
            
            //associated models to include 
            var associatedUserPathway = context.app.services['user_pathway'].Model;
            var associatedPathway = context.app.services.learningpathway.Model;
            var associatedSubjectLevel = context.app.services.subjectlevel.Model;
            var associatedSubject = context.app.services.subject.Model;
            var associatedUserContent = context.app.services['user_content'].Model;
            var associatedContent = context.app.services.content.Model;      
            //map association includes
              context.params.sequelize = {
              raw:false,
              include: [
                {
                  model:associatedUserPathway,
                  include:[
                    {
                      model:associatedPathway,
                      include:[
                        {
                          model:associatedSubjectLevel,
                          include:[
                            {
                              model:associatedContent,
                              include:[
                                {model:associatedUserContent}
                              ]
                            },
                            {
                              model:associatedSubject
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ] 
            };
        }
        // delete any special query params so they are not used
        // in the WHERE clause in the db query.
        delete context.params.query.include;
        delete context.params.query.$call;
        //return context;
      }
     ]

After Hook

get: [
       async function (context) {
        log.info("calling: " + rootServiceName + "get - after");
        //set input data to modify
        var inputData = context.result;
   
   // //--call functions--
      //high level learning pathway information
      function tier1data(){
       //some data processing on inputData
       
        }

        //learning pathway info with subject levels
        function tier2data(){
         //some data process on inputData   
        }

        //Call for basic learning pathway information
        if (includeValue==1) {
          tier1data();
        }
        else if (includeValue==2) {
          tier2data();
        }else if(includeValue){
          context.result="Invalid Call Type";
        }
         
        //return context  (async so that this occurs before function finishes)
        return Promise.resolve(context);
      }
    ]

Expected behavior

The expected behavior is that whenever I make a call with include=1 or 2, the after hook should run and return the processed data.

Actual behavior

When tested, this worked fine for individual calls in POSTMAN. I would always receive the processed data (based on the data processing in the after hook).

However in cases where we are using this API in a front end (we are using axios for REST calls), if there is a situation where API calls for the same endpoint are run in quick succession, intermittently the data is received as if only the before hook has run and not the after hook.

System configuration

Tell us about the applicable parts of your setup.

Module versions (especially the part that's not working):
"@feathersjs/configuration": "^2.0.6",
"@feathersjs/errors": "^3.3.6",
"@feathersjs/express": "^1.3.1",
"@feathersjs/feathers": "^3.3.1",
"@feathersjs/socketio": "^3.2.9",
NodeJS version:
v10.15.1
Operating System:
Windows 10

Browser Version:
Chrome 75.0.3770.142

@daffl
Copy link
Member

daffl commented Jul 18, 2019

This usually means that something in your processing is either

  • Relying on a global variable that is referenced by all requests which it shouldn't
  • Async functions are not waited for properly

@ivanELEC
Copy link
Author

Thanks for the swift reply.

I think your first point is the issue. I'm storing includeValue globally in the hook's .js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants