Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
How to search an array of objects obtained by axios for an id? Vue 2

I am trying to verify if the user is inside that list that I capture by axios, the issue is that I have used the FILTER option but it always returns undefined or [], being that if the user exists in that array.

I can't think what else to do, because I validate if it is by console.log() the variable with which I ask and if it brings data.

    created() {
        this.getStagesDefault()
        this.getSalesman()
        this.getStagesAmountByUser()
    },
    methods: {
        async getSalesman(){
            const { data } = await axios.get('salesman')
            this.employees = data.data
        },
        getStagesAmountByUser(){
            console.log(this.user['id'])
            var objectUser = this.employees.filter(elem => {
                return elem.id === this.user['id']
            })

            console.log(objectUser)
        },

Console

enter image description here

Vue data

enter image description here

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The method getSalesman is asynchronous, meaning that getStagesAmountByUser will start executing before getSalesman finishes.

Two ways to fix the problem:

  1. Await the getSalesman method, but you have to make the created method async as well. Change the code as follows:
async created() {
    this.getStagesDefault()
    await this.getSalesman()
    this.getStagesAmountByUser()
}
  1. Attach a .then to the getSalesman function, and start the next one inside the .then. Change the code as follows:
created() {
    this.getStagesDefault()
    this.getSalesman().then(() => this.getStagesAmountByUser())
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

getSalesman is an async method. At the time of the filter, the array being filtered is still empty.

this.getSalesman()            // this runs later
this.getStagesAmountByUser()  // this runs right away

Have the methods run sequentially by awaiting the async method:

await this.getSalesman()
this.getStagesAmountByUser()
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can avoid the inefficient clientside filtering if you pass the id to the backend and only select by that id.

Additionally, created only gets called once unless you destroy the component which is also inefficient, so watch when user.id changes then call your method again.

Plus don't forget you must wrap any async code in a try/catch else you will get uncaught errors when a user/salesman is not found etc, you can replace console.error then with something which tells the user the error.

{
  data: () => ({
    employee: {}
  }),
  watch: {
    'user.id' (v) {
      if (v) this.getEmployee()
    }
  },
  created() {
    this.getEmployee()
  },
  methods: {
    getEmployee() {
      if (typeof this.user.id === 'undefined') return

      try {
        const {
          data
        } = await axios.get(`salesman/${this.user.id}`)

        this.employee = data.data
      } catch (e) {
        console.error(e)
      }
    }
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda