Recover content from strapi using nuxtjs api

System Information

  • Strapi Version3.
  • Operating System: MacOs Big Sur
  • Database: MariaDB
  • Node Version:v14.17.0
  • NPM Version: 6.14.13
  • Yarn Version: 1.22.5

Hi everybody, I have some difficulties to recover content inside a strapi table in a Nuxtjs API.
I'd like to recover content in locations: location_id: 49 this is the table:

[
  {
    id: 2,
    title: 'Legal notice',
    description: 'Hello',
    date: '2021-06-01',
    locale: 'fr-FR',
    created_at: '2021-06-21T20:27:23.000Z',
    updated_at: '2021-06-22T13:07:14.000Z',
    location: {
      id: 2,
      name: 'toto',
      location_id: 49,
      locale: 'fr-FR',
      created_at: '2021-06-21T20:30:31.000Z',
      updated_at: '2021-06-21T20:33:44.000Z'
    },
    localizations: [ [Object] ]
  },
  {
    id: 4,
    title: 'Test cell',
    description: 'csdcmsdml',
    date: '2021-06-06',
    locale: 'fr-FR',
    created_at: '2021-06-22T14:10:29.000Z',
    updated_at: '2021-06-22T14:10:29.000Z',
    location: null,
    localizations: [ [Object] ]
  }
]

2 answers

  • answered 2021-06-23 07:35 kissu

    You want to extract the first object, right?
    Let's say that our array is called array, this will do it.

    this.array.filter(element => element.location?.location_id === 49)
    

    ?. is useful here because of the location: null case, which will otherwise throw an error.
    It's called optional chaining.

  • answered 2021-06-23 09:33 MammaClaudia

    I want to recover description in all langues I wrote it and I need recover the content of description according with location

    this is a capture of my content-type on strapi: content type from strapi

    I have 4 fields, I need description and location. this is my request in nuxtjs:

    async asyncData({ app, i18n, $md }: any) {  
           const data = await app.$strapi.find('legal-notices', {
          _locale: i18n.localeProperties.iso })
    

    Actually I have the content of description in english and french but it is not linked with location, this is my problem.

    thanks for your attention and your help.