When using a file uploader to set an image on v-model in Vue JS, it sometimes results in

I am currently using Vue JS 2 to develop an image uploader functionality. The input in question has a change function that triggers a function and sets the selected file to the v-model property.

After logging the data, I noticed that only an empty object is being set instead of the actual image. This issue is also causing the Vee Validate rule to fail as it detects the data as empty.

What could be missing in my implementation?

This is the HTML code snippet for uploading a logo:

<validation-observer ref="brandCreationForm" v-slot="{ handleSubmit }">
  <form class="space-y-6" @submit.stop.prevent="handleSubmit(create)">
    <validation-observer :key="1" class="space-y-6">

      <pre>
        {{ form }}
      </pre>

      <!-- Rest of the HTML code omitted for brevity -->

    </validation-observer>
  </form>
</validation-observer>

A specific function within my script runs as follows:

<script>
export default {
  // Layout and data setup goes here
  methods: {
    // Create brand and upload image functions are defined here
  }
}
</script>

The issue arises when adding an image where the form should ideally contain all information related to the image, but instead shows up as an empty object as depicted in the screenshot below:

https://i.stack.imgur.com/YuIex.png

Attempts such as using JSON.stringify before setting the model have been made, but unfortunately without success.

Answer №1

The formData object contains only functions, making it appear as an empty object when rendered in the template. To display the content of formData in the template, you must call one of its prototype functions and show the results.

<div v-for="item in form.brand_logo.values()">
  {{ item }}
</div>

Check out this codesandbox link to see how selecting a file displays it in the template and console.

If you want to simplify your code like I did with focusing on the formData aspect, consider creating a reproducible example for better understanding.

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

What is the process for incorporating items from Slick Grid into a Multi Select TextBox?

Exploring the world of Slick Grid for the first time. Here is where I define my variables in JavaScript. var grid; var printPlugin; var dataView; var data = []; var selectdItems = []; var columns = [ { id: "Id", name: "Id", field: "Id", sortable: t ...

The React.js component search test encounters errors in locating components

I have been working on a React app that utilizes Redux and React-router. I am currently writing tests using React TestUtils, and I encountered an issue with the following test results: The first expect statement is successful: expect(nav).to.have.length(1) ...

Encountering a 404 error when trying to access the rxjs node_module

While attempting to compile an angular2 application, I encountered the following issue: Error: XHR error (404 Not Found) loading http://localhost:3000/node_modules/rxjs(…) systemjs.config.js (function(global) { // map tells the System loader whe ...

What is the best way to conceal a paragraph with v-if within the context of Vuex?

I have been working on a small app that involves clicking a button to hide a paragraph, but I am using vuex for implementation purposes. The Home.vue file contains the paragraph and the About.vue file has the button. I want the paragraph to disappear bas ...

Arrange data in JSON file based on job title (role name) category

My current code successfully outputs data from a JSON file, but I'm looking to enhance it by organizing the output based on the "Role Name". For example, individuals with the role of Associate Editor should have their information displayed in one sect ...

NestJS: Specify the data type for @Body()

Consider the code snippet below: @Post() public async createPet(@Body() petDetails: PostPetDto): Promise<any> { } In this scenario, the type of @Bod() petDetails defaults to plain/any instead of the declared type of PostPetDto. What is the recommen ...

Creating a dynamic display of flash files on a webpage through a unique combination of PHP, AJAX, and

Despite my lack of experience with Ajax and minimal java knowledge, I have a strong background in SQL and PHP. Even though I anticipate criticism for this question, I am determined to seek help. My primary objective is to rotate 4 flash SWF files randomly ...

What is the best way to eliminate HTML <li> bullets using codebehind?

When working in codebehind, I often create an HTML list using the following method: HtmlGenericControl list = new HtmlGenericControl("ul"); for (int i = 0; i < 10; i++) { HtmlGenericControl listItem = new HtmlGenericControl("li"); Label textLabel ...

JavaScript: Obtaining the month based on the week number and year

Can someone assist me in determining the month based on a given week number and year? For example: Week - 48 Year - 2023 Desired Output - 11 ...

Save a randomly generated string in the Session, which will be replaced when accessed through a different PHP file

I have created a form with a hidden input field, and its value is dynamically set using a function. <?php class Token { public static function generate(){ return $_SESSION['token'] = base64_encode(openssl_random_pseudo ...

What is the best way to make a selected link stand out with a highlight?

I'm having an issue with the code below that is supposed to keep the selected link highlighted, but it only flashes the green color on click. Can someone help me figure out what's going wrong here? #sidebarContent a:active{ background-colo ...

The contrastText property of the MUI React Theme palette is not functioning properly

I am working with MUI React to design a menu and I have utilized the AppBar component. I would like to customize it in the following way: brown.myBrown = '#544846'; const brownTheme = createTheme({ palette: { primary: { ma ...

Adding a data attribute to a group of elements derived from an array

Looking to enhance the functionality of a slick slider by creating a custom navigation with buttons that will work alongside the original navigation dots. To achieve this, I need to extract the 'data-slick-index' attribute from every fourth slid ...

Strategies for Dealing with 'No Search Results' in Your Search Functionality

I am currently facing an issue with displaying "No Results Found" when a user utilizes my search feature. The current problem is that "No Results Found" appears immediately on the screen and then disappears while a search query is being processed, only to ...

A guide on verifying a username and password via URL using JavaScript

As I work on developing a hybrid application using the Intel XDK tool and jQuery Mobile framework for the user interface, one of my current tasks is implementing a login function. This function involves simply inputting a username and password and clicking ...

Calculating the sum of values in a JSON array using a specific parameter in Typescript

A flat JSON array contains repetitive identifier, categoryId, and category: data: [ { "identifier": "data", "categoryId": "1", "category": "Baked goods", "product": "Aunt Hattie's", "price": "375" } ...

What is the best method for translating object key names into clearer and easier to understand labels?

My backend server is sending back data in this format: { firstName: "Joe", lastName: "Smith", phoneNum: "212-222-2222" } I'm looking to display this information in the frontend (using Angular 2+) with *ngFor, but I want to customize the key ...

Trouble accessing Vue component on the website

I attempted to execute the code from the Vue docs regarding components, but unfortunately, the component isn't appearing as expected. What could be causing this issue? <html> <head> <script src="https://cdn.jsdelivr.net/npm/vue&q ...

What is the process for incorporating a custom attribute into an element with Vue directives?

One of the challenges I'm facing is dealing with a custom attribute called my-custom-attribute. This attribute contains the ID for the element that needs to have the attribute added or removed based on a boolean value. Although I've implemented ...

JavaScript Error - Value Not Assigned

I'm having trouble formatting a graph in my code. I keep encountering an undefined error when using console.log to output the data. Here's the snippet of my code: $(document).ready(function () { var graphData = new Array(); $.getJSON("ds ...