cupcave
cupcave

Reputation: 55

Vue: pass value of methodA to methodB

Im trying to get the value of classlists into my uScore but it's giving me an error. How can i pass the value of method displayClasslists to uScore

 methods: {
    displayClasslists() {
      this.$Progress.start();
      axios
        .get("/api/labview/" + this.$route.params.placeName)
        .then(({ data }) => this.$Progress.finish((this.classlists = data)));
    },
    uScore: function() {
      const elements = this.classlists.map(e => e.id);
      console.log(elements);
    }
  },

this is what i get:
[Vue warn]: Error in mounted hook: "TypeError: this.classlists.map is not a function" This is my json classlists
enter image description here

Upvotes: 1

Views: 40

Answers (1)

David Weldon
David Weldon

Reputation: 64312

Here are a some suggestions:

  1. Because uScore doesn't take any arguments and it depends only on classlists, then it should probably be a computed property.
  2. Your current implementation of uScore doesn't return anything.
  3. You should initialize classlists to be an empty array to avoid the error.
export default {
  data() {
    return { classlists: [] };
  },
  computed: {
    uScore() {
      return this.classlists.map(e => e.id);
    },
  },
  methods: {
    displayClasslists() {
      this.$Progress.start();
      axios.get(`/api/labview/${this.$route.params.placeName}`).then(({ data }) => {
        this.classlists = data;
        this.$Progress.finish();
      });
    },
  },
};

Upvotes: 3

Related Questions