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

Aggregate doesn't fire "loaded" event or does not keep changes made in the loaded hook #26

Open
GianlucaCesari opened this issue Sep 7, 2022 · 1 comment

Comments

@GianlucaCesari
Copy link

I don't even know if this repo is still active, but it's worth a shot.

I have a "loaded" hook on a model that uses this mixin in which i calculate some properties and assign it to ctx.data, it works well in the project but when it comes to the aggregate functions seems to be not working, any idea what could it be?

Here some code:

this is the hook:

Listing.observe("loaded", async (ctx: any) => {
  if (ctx.data) {
	  if (ctx.options?.currentUser) {
		  const currentUser = ctx.options.currentUser;
		  let isLikedByYou = await Listing.app.models.LikedListing.findOne({
			  where: {
				  memberId: currentUser.id,
				  listingId: ctx.data.id,
			  },
		  });
		  ctx.data["isLikedByYou"] = !!isLikedByYou;
	  }
  }
});

And this is the remote method aggregation which doesn't seem to be keeping the properties i set in the hook:

...

const pipeline: any = [{
	$geoNear: {
		near: point,
		distanceField: "distance",
		maxDistance: radius * 6378.1 * 1000,
		query: query,
	},
}, {
	$skip: page * limit,
}, {
	$limit: limit,
}];

return {
	total: (await Listing.aggregate(count_pipeline))[0]?.total || 0,
	page: page,
	limit: limit,
	listings: await Listing.aggregate({
		include: ["agent", "agency"],
		aggregate: pipeline,
	}),
	userSearch: user_search,
}

I couldn't verify if the problem is that the loaded event isn't being fired or if the data is not persisted trough the flow but I suspect it's the first case

Anyway, if you have any more insight on what could it be causing this, it would be much appreciated, btw it's a great mixin, thanks for developing it

@Akeri
Copy link
Member

Akeri commented Sep 30, 2022

Hi Gianluca. Thanks for your interest in this mixin.

I agree with you that probably loaded hook is not being fired.
Personally I had many issues in the past with hooks in my LB3 projects, so I usually avoid them and replace by methods strategically called in the right places (it also gives me more control of what's happening).

Unfortunately there are no plans to mantain this repository, so I could not verify it.
However if you make some progress, please let me know.

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