Strapi findOne (i18n) in nuxt js need help

System Information
  • Strapi Version:
  • Operating System:
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

hi , I trying to learning strapi i18n connnect with nuxt js
however, I came across a challenge need helps from your guys

the thing is this
i can fech data with locale parameters from strapi to locale
by


export default {
async asyncData({ $strapi, i18n,store, error }) {
try {
const response = await $strapi.$products.find({_locale: i18n.locale })
store.commit(‘setProducts’, response)
} catch (e) {
error(e)
}
},
data() {
return {
products: [],
}

it’s work and also works for others probs

however when came to fineOne(id or slug)

I can not fetch data with with locale parameter for single products

For example,

export default {
async asyncData({ $strapi,i18n, store, route }) {

const currentProduct = await $strapi.$products.findOne(route.params.slug, {_locale: i18n.locale })
return { currentProduct }

},
data() {
return {
currentProduct: {},
}
},

I am using strapi nuxt offical plugin , so has no idea how to fetch single data correctly by with findOne(id) or findOne (slug)with locale parameters

Your help is much appreciated

thanks in advance

Kevin

Hi!

I have the exact same issue. Is someone find out how to fix it? Thank you!

Hello, you should pass an object to findOne and find, for find you passed the object correctly, but for findOne you are currently passing two parameters.

Can you try:

findOne({id:route.param.id, _locale: i18n.locale}) 

Hi Sunnyson,

I tried it but it doesn’t work… I think the findOne method doesn’t implement the localization param.

Maybe in a further version

I had the same problem with findOne and ?_locale. I created controller logic that works for my project. This works if all localizations for the page share the same slug. I can also show how I did that if needed.

const { sanitizeEntity } = require('strapi-utils');

module.exports = {

  async findOne(ctx) {
    const { slug } = ctx.params;
    const { _locale }  = ctx.query;

    // a request example would be 'http://localhost:1337/blog-posts/blog-post-1?_locale=fr-CA'
    // the findOne by slug AND locale logic
    if (_locale) {
      const localeEntity = await strapi.services['blog-post'].find({ _locale, slug })
      return sanitizeEntity(localeEntity, { model: strapi.models['blog-post'] });
    };


    // the findOne by slug logic
    const entity = await strapi.services['blog-post'].findOne({ slug });
    return sanitizeEntity(entity, { model: strapi.models['blog-post'] });
  },
};