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

110
Visualizações
Calling method in my store returns empty observer object

I am new with Vue.

I have a Vue component like below. The return value of my function getBuildingsByOwnerRequest is unexpected: It returns an empty observer object. Only if I run getBuildingsByOwnerRequest again I receive the expected output from my store action.

Could this be a reactivity problem?

data() {
    return {
        editedOwner = {
            "owner_id": 12223,
        }
    },
}

computed: {
    ...mapState("buildings", ["buildings_by_owner"]),
},

methods: {
    ...mapActions("buildings", ["getBuildingsByOwnerRequest"]),
   
    function() {
        this.getBuildingsByOwnerRequest(this.editedOwner.owner_id);
        console.log(this.buildings_by_owner) // returns empty observer object ([__ob__: Observer] with length: 0)

        // if I run the function again I get the expected return
    };
}

buildings.js (store):

state: {
    buildings_by_owner: []
},

actions: {
    getBuildingsByOwnerRequest({ dispatch }, owner_id) {
    axios
        .get(
            `${process.env.VUE_APP_BASE_URL}/get/buildings_by_owner/owner_id=${owner_id}`
        )
        .then((res) => {
            // API call returns valid json as expected
            dispatch("receiveBuildingsByOwner", res.data);
        });
    },

    receiveBuildingsByOwner({ commit }, payload) {
      commit("RECEIVED_BUILDINGS_BY_OWNER", payload);
    },
}

mutations: {
    RECEIVED_BUILDINGS_BY_OWNER(state, payload) {
      state.buildings_by_owner = payload;
    },
}
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

The object is empty at the time it's logged. All asynchronous actions should return a promise:

getBuildingsByOwnerRequest({ dispatch }, owner_id) {
  return axios
  ...

A promise needs to be awaited before accessing results that it promises:

 this.getBuildingsByOwnerRequest(this.editedOwner.owner_id).then(() => {
   console.log(this.buildings_by_owner)
 })
about 4 years ago · Santiago Gelvez 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