Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am looking to push to an array from within a method in Vue.js 2, Vue is being used within Laravel and I am getting the following response. Could there be a better way of doing it?

Uncaught TypeError: _vm.createSelection is not a function

What I wish to create is:

selection[
{food: Chicken, quantity: 3},
{food: Rice, quantity: 2},
{food: Pasta, quantity: 1}
];

The following code is being used:

<template>
  <div>
    <div>Credits carried through: {{ credits }}</div>
    <div v-for="meal in meals">
      {{meal}}
      <input :id="meal" :name="meal" v-model.number="creditsPerMeal[meal]" type="number" v-on:input="createSelection()">
    </div>
    <div>
      Credits used: {{creditsSum}}/{{credits}}
    </div>
  </div>
</template>

<script>
  export default {

    mounted() {
        console.log('Component ready.');

        console.log(JSON.parse(this.f));

    },

    props: ['f','c'],

    name: 'credits',
    data: function () {
     var meals = JSON.parse(this.f)

     var creditsPerMeal = {}
     for (var i = 0; i < meals.length; i++) {
       creditsPerMeal[meals[i]] = 0
     }

     var createSelection = []


      return {
        credits: this.c,
        meals,
        createSelection: [],
        creditsPerMeal
      }
    },
    computed: {
      creditsSum () {
        return Object.values(this.creditsPerMeal).reduce((a, b) => a + b, 0)
      },
    },
    methods: {
       createSelection (){
           for (var i = 0; i < meals.length; i++) {
               createSelection.push({
                  food: meals[i],
                  quantity: creditsPerMeal[meals[i]]
               })
           }
       }
     }
  }
</script>

UPDATED METHOD

methods: {
   createSelection (){
       for (var i = 0; i < JSON.parse(this.f).length; i++) {
           this.createSelection.push({
              food: JSON.parse(this.f)[i],
              quantity: creditsPerMeal[JSON.parse(this.f)[i]]
           })
       }
   }
 }
share|improve this question
    
as a guess: this.data.createSelection.push(...)? How would you except the method createSelection to access a local variable from a different function scope? – Balázs Édes Jan 18 at 0:23
    
No luck unfortunately @BalázsÉdes, I am really struggling with it. Almost need to create some kind of bind. I cant think of another way. – James Parsons Jan 18 at 0:26
up vote 1 down vote accepted

Your data method is creating an array property called createSelection, which is likely shadowing/replacing the createSelection method you're defining. Make sure you're using unique names for all members.

share|improve this answer
    
Thanks Jacob, that has made it closer but there is still an issue padding creditsPerMeal. Im getting the following error. Uncaught ReferenceError: creditsPerMeal is not defined. Is there a way to pass this? It could be the value of the input. Could that work? – James Parsons Jan 18 at 1:05
    
In fact that would make them all the same? – James Parsons Jan 18 at 1:07
    
Maybe this.creditsPerMeal is what you want. – Jacob Jan 18 at 1:19
    
Im still getting an error, Uncaught TypeError: _vm.createSelection is not a function – James Parsons Jan 18 at 1:29
    
Did you undo createSelection not being replaced with the array? – Jacob Jan 18 at 2:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.