const cards = await strapi.query('api::card.card').findMany({ where: { cardset:…
- User
const cards = await strapi.query('api::card.card').findMany({ where: { cardset: cardset.id }, limit: 5, orderBy: { id: 'random' } }); 这个能随机获取五张卡牌吗? 不能的话,帮我修改下
- Assistant
The code snippet provided is using Strapi's querying syntax to fetch data from a collection of 'cards' that belong to a specific 'cardset'. However, the
orderBypart with the value'random'is not a valid option in Strapi's standard query language.To fetch random records from the database, you need to handle the randomness at the database level or fetch all records and then select random ones inside your application logic. Strapi does not provide a built-in way to order by random directly.
Here's an alternative approach using JavaScript logic to pick random cards after fetching the data: