Skip to content

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

@GianlucaCesari

Description

@GianlucaCesari

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions