Jinghang
Jinghang

Reputation: 33

Trouble finding specific data in Vue component with method within the same component

I am making a request management system for my company. The requirements are:

-Able to add a new row of the request.

-Choosing the description of the request will generate the parameters to be chosen. Parameters are on the same row as its respective description.

-Store the description and parameter as an array.

To approach this, we've used vue.js to do scripting within a blade template in the Laravel framework.

Vue.component('request', {
    props: ["index"],
    // Template for every individual row of test
    template: `
    <tr>
        <td>@{{ index }}</td>
        <td>
            <select  :name="description" @change="populate" required>
                <option value="" selected disabled>--Select--</option>
                @foreach ($types->sortBy('description') as $types)
                <option value="{{$types->description}}">{{$types->description}}</option>
                @endforeach
            </select>
        </td>


        <td>
            <select  :name="parameter" required  >
                <option >@{{ shared.parameter.parameter1 }}</option>
                <option >@{{ shared.parameter.parameter2 }}</option>    
                <option >@{{ shared.parameter.parameter3 }}</option>
            </select>
        </td>
    `,
    data(){
        return{
            // bind the name field of the form, for submission
            shared: store,
            description: 'tests['+this.index+'][description]',
            parameters: 'tests['+this.index+'][parameter]',
            something: 2,
        }
    }
    ,
    methods: {
        populate: ()=>{
            var self = this.index;
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                url:'/parametersByDescription',//this is specified in web routes
                type: 'POST',
                data: {description: this.description},
                success: function(data){
                    store.parameter = data;
                }
            })
            return;
        }

    }

})
let store = {
    parameter: [],

index increases with a function in methods of root. A new row is also added when done so. The whole basis of adding another row is the reason a large chunk of the form is generated by the template in vue.component request. The populate function sends my description through data: to the function in my controller specified by the URL.

This is where I start having problems. I need to parse the description I have selected in the form in the populate function, however I can't find the specific term to use. In Vue Devtools, I can see description as one of the data values but I get Uncaught TypeError: Cannot read property 'description' of undefined when I try to parse this.description. I have also hard-coded the value of 2 into something and tried to parse it, however the same error appears. I just need to get this particular description value and everything else should run smoothly. Thank you for your time.

Upvotes: 0

Views: 56

Answers (2)

Jinghang
Jinghang

Reputation: 33

I made a simple change in syntax together with the suggestion made by @Quinten and it works now.

   data: function(){
        return{
            // bind the name field of the form, for submission
            shared: store,
            description: 'tests['+this.index+'][description]',
            parameters: 'tests['+this.index+'][parameter]',
            something: 2, //some placeholder value, I am adding another variable in my actual code along with the template of the component
        }
    }
    ,
    methods: {
        populate: function(){
            var self = this.something;
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                url:'/parametersByDescription',//this is specified in web routes
                type: 'POST',
                data: {description: self},
                success: function(data){
                    store.parameter = data;
                }
            })
            return;
        }

    }

})

Upvotes: 1

Quinten
Quinten

Reputation: 114

The this in this.description refers to the ajax object, declare let self = this; so it becomes self; self.description.

Also as a side note, use Axios instead of Ajax, it saves you from a lot of trouble.

Upvotes: 1

Related Questions