Tips for showcasing a photo collection using Nuxt and Strapi

Seeking advice on displaying images from a gallery page sourced from Strapi. Previous methods have not been effective in my case.

My dynamic gallery page successfully retrieves collections from Strapi, but I'm struggling to showcase the images correctly.

I've retrieved all the details for my gallery, but I'm unsure how to utilize them with the v-for directive.

Here's how I fetch the specific gallery (_slug.vue page)

async asyncData({ $strapi, params }) {
    const gallery = await $strapi.find("galleries", {
      slug: params.slug
    });
    console.log(gallery);
    return {
      gallery
    };
  },

And here is my attempt at displaying it:

<h2 class="text-2xl font-normal text-gray-600 mt-12 mb-3">{{ gallery.title }}</h2>
      <p class="mb-10 text-lg font-light text-gray-700">{{ gallery.description }}</p>
      <div class="inline-flex flex-wrap">
        <div v-for="item of gallery" class="w-full md:w-1/2 lg:w-1/3 xl:1/4 p-4">
          <img
            class="w-full h-96 object-cover rounded transition ease-in-out transform hover:-translate-y-1 hover:shadow-xl hover"
            :src="$config.strapiUrl + item.gallery.images[0].url"
            alt=""
          />
        </div>
      </div>

The $config.strapiUrl URL variable works perfectly on other pages. The issue seems to lie in how I'm handling the data. I can't retrieve any images, 'title', or 'description', although they are present in the API response.

Below is the sample API response:

[
{
    "id": 6,
    "title": "Gallery 2",
    "description": "Description here",
    "published_at": "2021-10-23T20:47:50.752Z",
    "created_at": "2021-10-23T20:47:46.503Z",
    "updated_at": "2021-10-23T20:53:51.527Z",
    "slug": "gallery-2",
    "images": [
        {
            ...
        }
    ],
    "cover_image": {
        ...
    }
}

]

Answer №1

After tackling the issue, here's how I successfully resolved it:

The problem stemmed from incorrectly calling slug on the function instead of relying on the ID when working with Strapi.

To rectify this, I modified the findOne function to retrieve the slug rather than the ID by referring to a helpful video tutorial found here.

Here is the updated code snippet:

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

module.exports = {
  /**
   * Retrieve a record.
   *
   * @return {Object}
   */

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

    const entity = await strapi.services.restaurant.findOne({ slug });
    return sanitizeEntity(entity, { model: strapi.models.restaurant });
  },
};

Subsequently, I was able to fetch the correct data for both the title and description.

In Nuxt, I updated the v-for loop to:

v-for='image of gallery.images'

And made adjustments to the image tag as follows:

<img class="w-full h-96 object-cover rounded transition ease-in-out transform hover:-translate-y-1 hover:shadow-xl hover" :src="$config.strapiUrl + image.url" alt="" />

A special mention goes out to @IAmRoot from Strapi - Discord for their assistance throughout the troubleshooting process.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble testing a basic Vue example component in Laravel 5.4

I recently installed Laravel 5.4 and after running npm install, I tested a vue component example but nothing happened as expected. After running npm run dev in my project directory, the compilation was successful. However, when I ran php artisan serve, al ...

Set the :value for a specific member in an array or object within a Vue 3 - Vuetify input component

I am attempting to retrieve the value of different inputs such as combo boxes, text fields, and switches on these dynamic forms, but I am not having any success with it. Custom Template: VDialog(v-model='vDialogTrigger') VCard VCardTitle ...

Components in Vue are not being displayed correctly when rendered in a separate blade file

Currently, I am in the process of developing a Laravel application with Vue.js. Although I am new to Vue.js and still learning, I have successfully implemented two interfaces within my application - one for admin panel and another for normal users. The cha ...

When the component is initialized, the computed property is not being evaluated

My maps component initializes a Google map, adds markers based on props passed from the parent, and sets the correct bounds of the map. However, the markers are added through a computed property to make it reactive. Everything seems to be working fine, exc ...

Vue for Number Crunching

Learning vueJS is quite new to me. I am attempting to capture two input values, add them together, and display the result. I have encountered a strange issue where when subtracting number1 from number3, multiplying number1 with number2, or dividing number ...

Discovering Geo Coordinates with Vue.js

To ensure accuracy, I am setting a 10,000 millisecond timeout to retrieve the client's current geolocation. var options = { timeout: 10000 }; This function is written in JavaScript: function getCoordinates() { return new Promise(functi ...

Returning a value with an `any` type without proper validation.eslint@typescript-eslint/no-unsafe-return

I am currently working on a project using Vue and TypeScript, and I am encountering an issue with returning a function while attempting to validate my form. Below are the errors I am facing: Element implicitly has an 'any' type because expression ...

Tips on incorporating worldwide error handling in Vue

Trying to achieve global error handling in Vue.JS similar to Angular 2+ has been a challenge. Despite numerous attempts, I have yet to find a satisfactory approach to implement this handling effectively. Picture having multiple service methods that need t ...

Ensuring the correct order of script, template, and style tags in Vue using a linter

Looking to implement a new linter rule that verifies the sequence of tags: <script>, <template>, and <style>. By default, Vue places <template> first, but I believe <script> should come first as it holds more importance. It w ...

Error: serialport in node_modules throwing unexpected token SyntaxError

I have been attempting to run the vue-electron app, but I keep encountering this error. App threw an error during load C:\newFolder02\pos4-desktop\node_modules\@serialport\stream\lib\index.js:103 const settings = ...

Creating a Docker image for a Vue.js application

I'm facing an issue while trying to build a docker image for my Vue.js app. Currently, I have a Vue.js application and I am looking to create a docker image for it. Here is the content of my Dockerfile: FROM node:7.7.2-alpine WORKDIR /usr/app CO ...

Utilizing Vue JS to showcase a pop-up block when hovering over a specific image

There are four images displayed, and when you hover over each one, different content appears. For example, hovering over the first image reveals a green block, while hovering over the second image reveals a blue block. However, the current logic in place i ...

Why is the [Vue alert]: Prop error caused by custom validator not passing for prop "value"?

I am working on a nuxt.js app that utilizes the vuejs-datepicker library: <template> <!-- ... --> <DatePicker :value="datePicker.value" /> <!-- ... --> </template> As part of my project, I have several date variables: ...

When using v-for to render an array list fetched from AsyncData, an error is thrown: The virtual DOM tree rendered on the client-side does not match the one

For my application, I am utilizing Nuxt.js. On one of the pages, I am using AsyncData to fetch an array of data objects from my API asynchronously. These data objects are then rendered in my template using v-for. Everything is functioning properly until I ...

What is the syntax for populating an attribute on the same line as v-for in Vue.js?

I am currently working on a simple loop utilizing Vue to iterate over an array of objects and populate table rows. <tr v-for="user in users"> <td>{user.name}</td> <td>{user.id}</td> </tr> However, I also need to as ...

What are the reasons for being unable to access the HTML canvas in Vue?

I am currently working on creating a canvas element in Vue, but I seem to be encountering issues with the canvas instance. It appears that I may not be declaring or utilizing it correctly, as I am receiving the following error: TypeError: Cannot set pro ...

Attempting to link the input field with a data value on a Vue page

I'm currently facing an issue with my table and binding the input element to the "project_settings" object. The goal is to have the values inside the project_settings object change based on whether the checkbox is checked or not. However, every time I ...

Is the popup not opening with just one click?

https://i.stack.imgur.com/09dcf.png Upon clicking the first input cell, the popup opens and closes as expected. However, when closing the initial input and opening another one, an orange mark icon appears but the popup doesn't open until the second c ...

Information regarding gender vanishes upon refreshing the page

When the page is refreshed, the variable called jso disappears. Is there an alternative method for storing information that does not involve using a button? The goal is to have it work seamlessly when the page is reloaded without requiring any user action. ...

Display a modal following a successful HTTP request

As I reflect on this code, I can't help but cringe at its ugliness. Forgive me for what you are about to see! The goal is to make an HTTP request using Axios and then display a modal confirming that an email has been successfully sent. Currently, th ...