Vuejs select isn't choosing the correct options

Whenever I navigate to the edit page, I expect all my saved product tags to be automatically selected by default.

Currently, only one tag is getting selected and repeated for all the tags I have (example)

https://i.sstatic.net/Bj6Kk.png

Code

HTML

<el-col :span="22">
    <el-select
        v-model="form.tags"
        multiple
        filterable
        style="width:100%;"
        allow-create
        default-first-option
        placeholder="Choose tags for your product">
        <el-option
            v-for="item in tagss"
            :key="item.id"
            :label="item.name"
            :value="item.name">
        </el-option>
    </el-select>
</el-col>

<el-form-item label="Categories">
    <el-col :span="24">
        <el-cascader
            v-model="form.categories"
            style="width: 100%;"
            :options="cats"
            :props="{
                multiple: true,
                checkStrictly: true,
                value: 'id',
                label: 'name',
            }"
            clearable
            filterable>
        </el-cascader>
    </el-col>
</el-form-item>

Script

data() {
    return {
        cats: [],
        tagss: [],
        form: {
            tags: [],
            categories: [],
            _method: 'PUT',
        },
    }
},
mounted () {
    this.fetchProduct()
    this.getData()
},
methods: {
    getData () {
        axios.get('/api/admin/products', {
            headers: {
            Authorization: 'Bearer ' + localStorage.getItem('access_token')
            }
        })
        .then(response => {
            this.brands = response.data.brands;
            this.cats = response.data.categories;
            this.tagss = response.data.tags;
        })
        .catch(function (error) {
            console.log('error', error);
        });
    },
    fetchProduct() {
        axios
            .get('/api/admin/products/'+this.$route.params.id, {
                headers: {
                    Authorization: 'Bearer ' + localStorage.getItem('access_token')
                }
            })
            .then(response => {
                this.form.tags = response.data.data.tags
                this.form.categories = response.data.data.categories
                console.log('all tags from backend: ', this.tagss) // sample data below
                console.log('this product tags from backend: ', response.data.data.tags) // sample data below
            })
            .catch(function (error) {
                console.log('error', error);
            });
    }
}

Here are the sample results of my console.log mentioned above

https://i.sstatic.net/B86ov.png

Note: Although I discussed the issue with my tags, I am also sharing an example related to my categories code, as both appear to have similar issues.

Do you have any ideas on what could be causing this problem?

Answer №1

Question Resolved

Final version of the code

this.form.tags = response.data.data.tags.map(row => row.name);
this.form.categories = response.data.data.categories.map(row => row.id);

Outcome

https://i.sstatic.net/rXnN5.png

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

Learn how to utilize ng2-file-upload in Angular for uploading .ply files effortlessly!

I'm currently working on uploading various files using ng2-file-upload. I've been successful in uploading different file types like png and jpg, but I'm facing an issue with the .ply file extension. Can someone guide me on how to upload a fi ...

How can one dynamically update a page in Angular when the path is changed?

I am facing a pagination issue in Angular. Here is my HTML code: <!-- A div element for pagination part at the bottom of the page --> <div style="margin-left: 50%; margin-top: 20px; margin-bottom: 20px"> <ul class="paginat ...

Creating HTML Elements using Typescript's Syntax

Looking for the most effective method to define HTML elements in typescript is a challenge I am facing. One particular issue that keeps arising is when dealing with arrays of DOM nodes retrieved using document.querySelectorAll. The type assigned to these e ...

Display a Three.js scene within a Vuetify.js component

Struggling to integrate a basic Three.js scene with the Vuetify v-app component? You're not alone. By using vue, vue-router, and three-js without Vuetify, I encountered no issues loading a simple 'Hello World' scene. If you're curious, ...

Having trouble with Nestjs Global Validation Pipe when parsing Boolean query parameters?

Within this controller, I am attempting to pass a boolean parameter in the GET call. @Controller('tests') export class TestController { constructor(private readonly testService: TestService) {} @Get() async getTests(@Query() params: ...

text:js Using a StringBuilder

As a newcomer to the .NET platform, I am eager to learn how to pass a JS function with curly brackets as a string inside StringBuilder.AppendFormat() in C#. Below is my desired approach: StringBuilder sb = new StringBuilder(); sb.AppendFormat(@"<s ...

The error message "Cannot read property 'properties' of undefined" is being thrown by the leaflet-search plugin

I attempted to implement the leaflet-search feature using a Vue-cli component. However, when I initiate a search, I encounter the following error message specifically related to the leaflet search function: Uncaught TypeError: Cannot read property &apo ...

retrieve a string from a given array

I need to retrieve a string from an array in vue and display it on the screen. Here is the method I created for this purpose: displayFixturesName() { const result = this.selectedFixture.toString(); document.getElementById(& ...

Utilize VueJS to upload and visualize a file input on your website

I am currently working with TypeScript and Haml in conjunction with vue.js. My goal is to enable users to upload and view a file seamlessly using the vue.js framework. I have successfully managed to upload an image, however, I am facing an issue where the ...

You will encounter an error labeled as "Modifiers cannot appear here.ts(1184)" when attempting to utilize getStaticProps in a

For some reason, whenever I implement the getStaticProps function in my component, an error pops up: https://i.sstatic.net/0R4vE.png However, if I move the function above my component declaration, the error magically disappears! According to the document ...

Is there a way to implement a select all feature for checkboxes that have been manually toggled on and off?

I found a solution to select all check-boxes by manipulating the input elements with their class names and toggling the "checked" attribute. However, I encountered an issue where if I individually checked and unchecked any of the check-boxes, they would no ...

A Typescript type that verifies whether a provided key, when used in an object, resolves to an array

I have a theoretical question regarding creating an input type that checks if a specific enum key, when passed as a key to an object, resolves to an array. Allow me to illustrate this with an example: enum FormKeys { x = "x", y = "y&q ...

Updating array content based on property criteria

I've been tackling a project in Angular where I have two arrays set up like this: array1 = [ { Name: "Jack", Id: "1", Location: "UK" }, { Name: "Rose", Id: "2", Location: ...

What are the reasons behind lang sass not functioning within the style tag of a .vue file?

Even though I had previously installed sass-loader and node-sass in my project, I encountered an issue when attempting to use <style lang="sass"> in my vue file. The style did not compile as expected, however it worked perfectly without the lang="s ...

Tips for referencing functions in TypeScript code

In regard to the function getCards, how can a type be declared for the input of memoize? The input is a reference to a function. import memoize from "fast-memoize"; function getCards<T>( filterBy: string, CardsList: T[] = CardsJSON.map((item, i ...

What is the reason for it passing the type check?

I'm really struggling to understand this part: import axios from 'axios' import * as TE from 'fp-ts/lib/TaskEither' export const getIntent = (sessionId: string, input: string) => process.env.INTENT_URL ? TE.tryCatch( ( ...

Creating a high-quality select input component in Vue 3 that functions effectively

Struggling to implement pagination using a select field, I am unsure how to properly use the v-model and value properties. Below is my current implementation: This is the select/pagination section in the component: <p v-on:click="back" ...

Issue encountered with @Nuxt.js/Cloudinary plugin for Media Enhancement: "Access to this endpoint is restricted due to insufficient permissions"

I am currently utilizing the @nuxtjs/cloudinary module based on the module guide and a course video. However, I am encountering an error in the response as follows: Status 403, message: You don't have sufficient permissions to access this endpoint&qu ...

Tips for setting up CORS on the Vite server to eliminate CORS issues while loading images

In my Vite/Vue 3 application, I encounter an error while trying to load images. The error message reads: Access to image at 'https://website.com/image-url.jpg' from origin 'https://my-web-app-url.com' has been blocked by CORS policy: ...

What is the best way to import a TypeScript file in index.js?

I've recently developed an application using the react-express-starter template. I have a "server" directory where the backend is created by nodejs, containing an index.js file: const express = require('express'); const app = express(); c ...