Use find() and findOne() in custom controller?

System Information
  • Strapi Version: 4.0.2
  • Operating System: Mac
  • Database: sqlite
  • Node Version: v14.18.1
  • NPM Version: 6.14.15

Hi, I want to get a random question from my “question” collection type.
This is what I have tried:

async findOneRandom(ctx) {
// get data
const { data } = await super.find(ctx);
// get random id
const randomId = (data[data.length * Math.random() | 0]).id
// get random question
const question = await super.findOne(randomId);
return question;
}

The error I get:
TypeError: Cannot destructure property ‘id’ of ‘ctx.params’ as it is undefined.
Seems like I need to have the ctx from findOne(), but I only have the ctx from find().

Anybody can help? That would be great! :slight_smile:

Hi !
Did you found any solution to this problem? I encouter the same issue… I really don’t know why.

Thanks