-1

I want to display an array elements in my Vuejs template.

I am getting data from API, formatting them into an associative Array with a function and then trying to display in the template :

export default {
  components: {
    DefaultHeader,
    DefaultFooter
  },
  data() {
    return {
      info: null,
    };
  },
  mounted() {
    this.refreshData();
  },
  methods: {
    refreshData() {
      infoService.getInfo(this.$route.params.id)
        .then(response => {
          this.info = FormatHelper.BInfoFormat(response.data);
        })
        .catch(e => {
          console.log(e);
        });
    }
  }
}

When i try to display the first element in the template with this code :

{{ this.info.name }}

I get the error :

Uncaught (in promise) TypeError: Cannot read properties of null (reading 'name')

But when I display the full Array Object at the exact same place in code, I can see that it's defined :

{{ this.info }}

shows :

{ "name": "B3", "call": "about", "G": "H" }

Also, I get the right data when i try to console log in my mounted() component function :

console.log(this.info.name)

What is wrong in the way I am trying to access data getting me this error ?

4
  • 1
    is this.info initially null and gets populated later? try {{ this.info?.name }} - without atual code, or at least enough to know what this.info is, there's many reasons this will occur Commented Jan 6, 2024 at 6:07
  • When i try {{ this.info?.name }} the display is right, i get the name without any error, what is it doing ? And yes, this.info is first defined at null. Commented Jan 6, 2024 at 6:18
  • 1
    is first defined at null and null has no properties, therefore the error - either initially defined it as {} rather than null (though this may break other logic) or use v-if to conditionally show .name or use what I suggested - the actual reason would be easy to describe with some meaningful code in your question Commented Jan 6, 2024 at 6:22
  • I’m voting to close this question because the author answered without accepting Commented Jan 6, 2024 at 18:22

1 Answer 1

0

Problem solved, thanks to comments :

in :

export default {
  components: {
    DefaultHeader,
    DefaultFooter
  },
  data() {
    return {
      info: null,
    };
  },
  mounted() {
    this.refreshData();
  },
  methods: {
    refreshData() {
      infoService.getInfo(this.$route.params.id)
        .then(response => {
          this.info = FormatHelper.BInfoFormat(response.data);
        })
        .catch(e => {
          console.log(e);
        });
    }
  }
}

replace :

  data() {
    return {
      info: null,
    };
  },

by :

  data() {
    return {
      info: {},
    };
  },

The initial define at null is breaking the Array methods I was trying to use in the template.

Sign up to request clarification or add additional context in comments.

2 Comments

marked it solved, otherwise it draws attention of solvers
I cannot accept my own answer, it requires 22 more hours to wait

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.