Search code examples
strapinuxt3.js

Nuxt3 Plugin Strapi: How to return only one result using FindOne with populate parameter?


Using Nuxt3 and the Strapi v4 Plugin to create a blog. Using the findOne function to retrieve ONE article.

However using the populate parameter to also retrieve relational parameters such as images:

findOne<Article>('articles', { populate: "images"}, id)

returns a list of ALL articles. I want only my one article!

How do I get only the result for my given ID including the relations (images) when using the findOne function with the populate paramter?


Docs I consulted:


Solution

  • Your query syntax is wrong. findOne takes id as the second parameter and options as the third parameter as below:

    Syntax:
    findOne(uid: string, id: ID, parameters: Params) ⇒ Entry
    
    Solution in Strapi:
    const entry = await strapi.entityService.findOne('api::article.article', 1, {
      fields: ['title', 'description'],
      populate: { category: true },
    });
    
    
    Solution in Nuxt Strapi:
    const entry = await findOne<Article>('articles', 1, {
      fields: ['title', 'description'],
      populate: { images: true },
    });
    
    Reference: