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

Unable to see aggregation query result from contextFilter in outputFn #416

Open
Khushboo-Mulani opened this issue Nov 6, 2020 · 7 comments
Labels

Comments

@Khushboo-Mulani
Copy link

Hi @florianholzapfel,

Although aggregation query executes and gives result in contextFilter, I am unable to access the same result in outputFn(req.erm.result). Here is the code snippet of contextFilter function:

contextFilter: function (model, req, next) {
	if (req.aggregationQuery) {
	        model.aggregate(req.aggregationQuery).then((result) => {
		    req.erm.result = result;   //here the result of aggregation query is seen correct
		    next(model)
	      })
        }
   }

Wherein, aggregationQuery has been added in request object in the preRead hook in this case.

Expectation:

     outputFn: (req, res, next) => {
          const result = req.erm.result;   //here the result should be the result of aggregation query
          const statusCode = req.erm.statusCode;
          res.status(statusCode).json(result);
     }
@Zertz Zertz added the question label Nov 6, 2020
@Zertz
Copy link
Collaborator

Zertz commented Nov 6, 2020

Just to be sure, can you verify outputFn is called after your aggregation query sets its result?

@Khushboo-Mulani
Copy link
Author

@Zertz yes, outputFn is being called after aggregation query sets its result, but the result there is just of simple model.find query

@Khushboo-Mulani
Copy link
Author

@Zertz Tried setting the result as req.erm.result = result; manually too, but it doesn't work that way

@Khushboo-Mulani
Copy link
Author

Hi @Zertz,
Anything I should be trying to access the aggregation result in outputFn, without having to explicitly set the result?

@Zertz
Copy link
Collaborator

Zertz commented Nov 9, 2020

The result you are setting in contextFilter gets overriden shortly after when the query runs.

I'm not sure exactly what you're trying to achieve but this should work:

contextFilter: function (model, req, next) {
  if (req.aggregationQuery) {
    model.aggregate(req.aggregationQuery).then((result) => {
      req.erm.aggregationResult = result;
      next(model)
    })
  }
}

outputFn: (req, res, next) => {
  const result = req.erm.aggregationResult;
  const statusCode = req.erm.statusCode;
  res.status(statusCode).json(result);
}

@Khushboo-Mulani
Copy link
Author

@zerts the find query shouldn't run at all in the first place when aggregate query is present and executed. How can I stop that from happening?

In my case, for a request, first aggregation query is executed, the result of which we are assigning to "req.erm.aggregationResult", then find query also executes for the same request.

Is model we are passing to callback in "next(model)" executing the find query, if yes, how can that behaviour be changed?

@Zertz
Copy link
Collaborator

Zertz commented Nov 10, 2020

Perhaps this can help?

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

No branches or pull requests

2 participants