Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

399
Visualizações
How can I use data defined in data() in other methods in Vue js?

First I defined Types, Severities, and Statuses as [] and returned them in data().
Then I filled them with data in the methods getTypes(), getSeverities(), and getStatuses().
I want to use Types, Severities, and Statuses in the method getName()(just has console.log() as an example for now).
I noticed when debugging getNames(), type in the second for loop is undefined. Is it because the method is using Type before it was assigned values in getTypes()? How can I make it work?

Note: Types, Severities, and Statuses do get assigned values in the methods getTypes(), getSeverities(), and getStatuses(), the issues is how to use the data in other methods.

<script>
import IssuesTable from '../MyIssuesPage/IssuesTable.vue'
import AddIssue from '../MyIssuesPage/AddIssue.vue'
import axios from 'axios'

export default {
  props: ['id', 'project', 'issuesList', 'index'],
  components: { IssuesTable, AddIssue },
  data() {
    return {
      Issues: this.issuesList[this.index],
      tab: null,
      items: [{ tab: 'Issues' }, { tab: 'Calender' }, { tab: 'About' }],
      Types: [],
      Severities: [],
      Statuses: [],
    }
  },

  setup() {
    return {
      headers: [
        { text: 'Title', value: 'title' },
        { text: 'Description', value: 'description' },
        { text: 'Estimate', value: 'time_estimate' },
        { text: 'Assignees', value: 'userid' },
        { text: 'Type', value: 'issueTypeId' },
        { text: 'Status', value: 'issueStatusId' },
        { text: 'Severity', value: 'issueSeverityId' },
      ],
    }
  },

  mounted() {
    this.getTypes(), this.getSeverities(), this.getStatuses(), this.getNames()
  },

  methods: {
    getTypes() {
      axios
        .get('http://fadiserver.herokuapp.com/api/v1/my-types')
        .then(response => {
          this.Types = response.data
        })
        .catch(error => {
          console.log(error)
        })
    },
    getSeverities() {
      axios
        .get('http://fadiserver.herokuapp.com/api/v1/my-severities')
        .then(response => {
          this.Severities = response.data
        })
        .catch(error => {
          console.log(error)
        })
    },

    getStatuses() {
      axios
        .get('http://fadiserver.herokuapp.com/api/v1/my-status')
        .then(response => {
          this.Statuses = response.data
        })
        .catch(error => {
          console.log(error)
        })
    },

    getNames() {
      for (var issue of this.Issues) {
        for (var type of this.Types) {
          if (issue.issueTypeId == type.id) console.log('test')
        }
      }
    },
  },
}
</script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First of all, use created() instead of mounted() for calling methods that fetch data.

Next, you need to call getNames() only after all fetch methods complete.

created() {
this.getTypes()
  .then(this.getSeverities())
  .then(this.getStatuses())
  .then(this.getNames());
}

In order to chain methods like this you need to put return statement before each axios like this

  getTypes() {
  return axios
    .get("https://fadiserver.herokuapp.com/api/v1/my-types")
    .then((response) => {
      this.Types = response.data;
    })
    .catch((error) => {
      console.log(error);
    });
}

In this component, I see you are receiving issuesList and index props from the outside. I cannot know those values but you can console.log both of them inside created() and see what is happening because issuesList[index] is undefined.

That probably means issuesList is an array and that index does not exist in that array.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda