6

I get an error: "Uncaught TypeError: this.thePerson is not a function" when I run this:

var myApp = new Vue({
    el: "#app",
    data: {
        person: [this.thePerson()]
    },
    methods: {
        thePerson: function() {
            return {
                personNickname: "Bob",
                personOwes: 0,
                paymentType: {
                    card: true,
                    cash: false
                }
            };
        }
    }
});

I can't figure out why!

P.S. I realize this is weird looking code but there's a reason why I'm using a function to return the array contents...

2 Answers 2

6

Turn data into a function.

var myApp = new Vue({
    el: "#app",
    data: function() {
        return {
            person: [this.thePerson()]
        }
    },
    methods: {
        thePerson: function() {
            return {
                personNickname: "Bob",
                personOwes: 0,
                paymentType: {
                    card: true,
                    cash: false
                }
            };
        }
    }
});
Sign up to request clarification or add additional context in comments.

5 Comments

this is the context of a function. You were using this outside of a function. If your code ran in the browser, this would be window, and not your component. Always declare data as a function, no matter what, even if you aren't using this. vuejs.org/v2/api/#data
Check out this slightly built-out code: jsfiddle.net/k0cxth61/1 The computed method is outputting "Undefined" but if "this" is now the correct context then why is it undefined?
You're not actually using the computed property. You're using the variable getFirstName which is undefined. You want this.getFirstName. Notice that it's a property, not a function/method, you don't call it with parentheses.
I see that error which I fixed but still no luck. I built this in a very clear CodePen for you to look at... codepen.io/rettoy/pen/ejoKmq
Seems like data is evaluated before computed is, so you can't reference computed properties in data. Which makes sense, since computed properties are derived values from data.
2

Do you try, assigning persons value on Vue's created method ?

according to your example:

    var myApp = new Vue({
  el: "#app",
  data: {
      person: []
  },
  methods: {
      thePerson: function() {
          return {
              personNickname: "Bob",
              personOwes: 0,
              paymentType: {
                  card: true,
                  cash: false
              }
          };
      }
  },
  created(){
    this.person.push(this.thePerson())
  }
});

1 Comment

Thanks! This actually worked. @eric-guan please see that this is how I solved the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.