Challenges encountered when trying to display images in Vue Express

I am currently working on an express vue application and facing an issue with displaying images in vue.

Within a vue page, I utilize axios.get in the vue js beforeMount hook to retrieve image data in base64 format from an API.

I store this data in an array. However, while texts appear without any problem, the images do not show up consistently. Sometimes after certain actions like editing code, the images briefly appear but then the issue persists.

Here is the vue js code snippet:

<template>
    <div v-for="(designImg, i) in designImgs" :key="i">
      <q-img
        :src="designImg"
        height="100px"
        class="bg-grey-4"
      />
    </div>
<template>
<script>
  data () {
    return {
      designImgs: null
    }
  },
  beforeMount () {
    this.$axios.get('shop/product', {
      params: {
        designId: this.$route.params.designId,
      }
    }).then((res) => {
      if (res.data.error) {
        console.log(res.data.error)
      } else {
        const designImgUrls = res.data.imageUrls.split(',')
        const mydesignImgs = []
        for (let i = 0; i < designImgUrls.length; i++) {
          this.$axios.get('/shop/image', {
            params: {
              url: designImgUrls[i]
            }
          }).then((res) => {
            const url1 = 'data:image/jpeg;base64, ' + res.data
            mydesignImgs[i] = url1
          }).catch(err => {
            console.log(err)
          })
        }
        this.designImgs = mydesignImgs
      }
    }).catch((err) => {
      console.log(err)
    })
  }
<script>

It's worth noting that the server provides us with the following data:

imageUrls: "uploads\imagee-1592862806606.jpg,uploads\imagee-1592862806654.jpg"

After fetching the image data, res.data contains the base64 representation of the image like this:

/9j/2wBDAAYEBQYFBAY...

Answer №1

Successfully resolved the issue at hand. Altered the method of gathering image data within mydesignImgs array. Implemented the following code:

mydesignImgs.push(url1)

rather than employing this comparison:

mydesignImgs[i] = url1

The rationale behind this change?

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

Is the next function triggered only after the iframe has finished loading?

First and foremost, I understand the importance of running things asynchronously whenever possible. In my code, there exists a function known as wrap: This function essentially loads the current page within an iframe. This is necessary to ensure that Jav ...

Guide on transferring a JSON string from a JSP page to a Spring MVC controller

I am facing an issue while trying to pass a simple json string to my controller. The json string is not being received at the controller end. I have other functionalities like inserting, deleting, etc., inside my controller code which work perfectly fine, ...

What could be causing the "Cannot POST /api/" error to occur when attempting to submit a form?

Having issues with my basic website and struggling to find a solution. As a complete beginner in this field, I am stuck and need some guidance. Accessing http://localhost:3000/class/create works perfectly fine when running the server. However, trying to a ...

Transform the array of strings into an array for data mapping purposes

My issue is as follows: const input = "[\"https://i.imgur.com/PAYXC2n.jpg\",\"https://i.imgur.com/bEfjjxA.jpg\"]"; I am looking to convert this string into an array, and the expected output should be : out ...

A peculiar issue is occurring in Vue.js where a value within a v-for loop seems to be losing association with the intended array

During my third day of working with Vue.js, I decided to create a basic application for requesting car keys in a service department. Although I am aware that there are areas in the code that could be improved, I encountered a specific issue that I need ass ...

What is the procedure for turning off hover color on an href element?

Working on a website that utilizes MUI components, I have incorporated href into the Tab elements within the navigation bar. <Tab label={element} id={index} sx={{display: {xs: 'none', md: 'inherit'}}} href={`#${navElements[element ...

Maintain the div in its current location when zooming the map

I created a multi-layer map and added a div to it. However, the issue arises when I zoom in on the image, causing the div to change its position unexpectedly. For example, if I place the div over India and then zoom and drag the image, the div ends up in U ...

Sorry, we couldn't locate the API route you are looking for

Within my Next.js project resides the file main/app/api/worker-callback/route.ts: import { NextApiResponse } from "next"; import { NextResponse } from "next/server"; type ResponseData = { error?: string }; export async function PO ...

What is the most effective way to trigger a jQuery datatable event after updates, rather than before?

After the default datatable updates like searching and sorting are finished, I am looking to update DOM elements. The issue is that the event listening currently fires before the actual sort or search completion. My goal is for my code to run after the s ...

What steps should I take to ensure the text layout algorithm effectively scales the text based on the "font size" option when using SVG paths?

I have included a fully functional JavaScript code snippet at the end of this post, demonstrating how Hebrew text can be displayed in a "stretched" layout design, as referenced here. The output shows the text in various font sizes: 32 The text appears mos ...

Issue with Argument Validation in Internet Explorer 11 while using Vue.js

Encountering an error in IE11 while the code works fine in Chrome, Mozilla, and Safari. The issue arises when trying to remove a car from the garage. The error message "Invalid Argument" pops up once I attempt to remove a car from the garage section withi ...

Switch out the current R shiny htmlwidget data for updated data

Recently, I encountered an issue with a htmlwidget in one of my R shiny applications. Each time the widget render function was called multiple times, it failed to replace the existing data with new data. This bug caused the old data to linger and the new d ...

How to convert a JSON response into a select box using VueJS?

I have a VueJS component where I need to populate a html select box with data from a JSON response. This is my VueJS method: getTaskList() { axios.get('/api/v1/tasklist').then(response => { this.taskList = this.data.taskList; ...

I am trying to include the Css Baseline from @mui/material in my project. However, even though it is present in my JSON file, I am encountering an error stating that '@mui/material' needs to be included in the project

Struggling to import Css Baseline from @mui/material, it's listed in my json but I keep getting an error saying '@mui/material' should be included in the project's dependencies. I've been stuck on this issue for a while now! { &q ...

How to use jQuery to locate an ID or form outside of its parent element

Is it possible to access an ID or form that is outside of its parent ID? For instance: <ul> <li> <a></a> </li> </ul> <div> <div> <div> <div> ...

A step-by-step guide on dividing items in an array with React

user.js import React, { useState, useEffect } from 'react'; import Axios from 'axios'; const RecommendationDetail = () => { const [images, setImages] = useState([]); useEffect(() => { fetchRecommendations(); }, []); ...

Ways to reveal a concealed element by simply clicking on a different element

Check out this example on JSFiddle: http://fiddle.jshell.net/ggw6hqsj/1/ Currently, I am facing an issue where: Clicking the first button hides the second button. However, I am struggling with making the hidden button reappear when the first but ...

tips for extracting a specific attribute value from an XML document

Within my C program, I am working with the following XML data: <apStats><command chart_num="0">750</command><command chart_num="1">400</command></apStats> . $.ajax({ type: "POST", dataType: "xml", url: ge ...

Setting up Tipsy with titlesorting

Recently, I attempted to incorporate the Tipsy jQuery plugin into my website to display title tags as attractive tooltips. However, I am facing issues with the plugin not working correctly. Can anyone offer assistance with this? Specifically, I am looking ...

Vue.js - encountering an issue with undefined response data

Greetings! I've encountered a script issue while trying to submit a form in VUE. The error message displayed in the developer panel states: "TypeError: error.response.data.errors is undefined". As a newcomer to Vue, I'm seeking some assistance as ...