Hey, I use strapi 4 and I have 2 collections
Collection 1 : links (id, body…)
Collection 2 : likes (id, postid, userid)
I want to count all likes for on links id
I try to do this with a controller. But I have 1 error : error: allPosts.find is not a function
TypeError: allPosts.find is not a function
And I don’t know why. Can you help me ?
'use strict';
/**
* link controller
*/
const { createCoreController } = require('@strapi/strapi').factories;
module.exports = createCoreController('api::link.link', ({ strapi }) => ({
// Method 2: Wrapping a core action (leaves core logic in place)
async find(ctx) {
const { data, meta } = await super.find(ctx);
const linkId = data.map((link) => link.id);
//count all like for each post
const allPosts = await strapi
.db
.query('api::like.like')
.count({
where: {
postid: { $in: linkId },
}
});
console.log("nombre de like dif",allPosts);
const data2 = data.map((link) => {
link.like = allPosts.find(
(like) => like.attributes.postid === link.id
);
link = {id:link.id, ...link.attributes, like:link.like};
return link;
}
);
return { data:data2 , meta };
},
}));```