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

229
Visualizações
Vue: Access values over api (nested)

Hi Stackoverflow Community,

In my template I try to show data which I receiver over an api call. When I log the api get response this is what I receive: enter image description here

Now I try to show the data in location with the following code: {{ userdetails.location.location_name }}

My script looks like this:

data() {
    return {
      userdetails: [],
    };
  },
  methods: {
    getUserData() {
      DataService.getUserById()
        .then((response) => {
          this.userdetails = response.data;
          console.log(response);
        }) 

...

  created() {
    this.getUserData();
  },

The strange thing is, I can see the location_name in my application. But I also receive an error:enter image description here

I think I made somewhere a mistake, but I can't find it. Do you have any ideas what I am doing wrong?

Thank you in advance for all your tipps!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

At the start, when the template tries to access userdetails.location.location_name, it is an empty list, so you will see the error. Only once the DataService updated userdetails will the nested properties exist.

The easiest way to prevent this is to start out with userdetails as something which is 'falsey' and then use v-if to determine whether to render:

<div v-if="userdetails">{{userdetails.location.location_name}}</div>

data() {
return {
  userdetails: undefined,
};

},

Note however that if your DataService could ever return a dict which doesn't have the location property, you will need to do more testing in v-if to determine if it can be rendered.

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