Search code examples
javascriptvue.jsvisual-studio-codevuejs2vue-cli

Vue CLI error: Property or method "data" is not defined on the instance


I am facing an issue in my Vue project while trying to render the data from local JSON. I have read instructions and followed them-- every step-- yet the error prevails. Since I am new to Vue and advanced programming itself, I seek help.

english.json

{
  "0": {
    "text": "This be a square",
    "color": "blue",
    "size": "small"
  },
  "1": {
    "text": "here lies a square",
    "color": "red",
    "size": "medium"
  },
  "2": {
    "text": "Tharr be a squaree",
    "color": "black",
    "size": "large"
  }
}

ExperienceText.vue

<template>
  <article class="content__box" :v-for="data in expJson">
    {{data.text}}
    <h6>{{data.text}}</h6>
    <h3>{{data.color}}</h3>
    <p>{{data.size}}</p>
  </article>
</template>
<script>
import json from "@/components/json/english.json";
export default {
  name: "ExperienceText",
  data() {
    return {
      expJson: json
    };
  }
};
</script>

Error

[Vue warn]: Property or method "data" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

---> <ExperienceText> at src/components/mainComp/subComp/ExperienceText.vue
       <ExperienceSection> at src/components/mainComp/ExperienceSection.vue
         <Experience> at src/components/mainComp/Experience.vue
           <Main> at src/components/Main.vue
             <Container> at src/views/Container.vue
               <App> at src/App.vue
                 <Root>

Show 60 more frames
vue.runtime.esm.js?2b0e:619 [Vue warn]: Property or method "data" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

---> <ExperienceText> at src/components/mainComp/subComp/ExperienceText.vue
       <ExperienceSection> at src/components/mainComp/ExperienceSection.vue
         <Experience> at src/components/mainComp/Experience.vue
           <Main> at src/components/Main.vue
             <Container> at src/views/Container.vue
               <App> at src/App.vue
                 <Root>

vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in render: "TypeError: Cannot read property 'text' of undefined"

found in

---> <ExperienceText> at src/components/mainComp/subComp/ExperienceText.vue
       <ExperienceSection> at src/components/mainComp/ExperienceSection.vue
         <Experience> at src/components/mainComp/Experience.vue
           <Main> at src/components/Main.vue
             <Container> at src/views/Container.vue
               <App> at src/App.vue
                 <Root>

vue.runtime.esm.js?2b0e:1888 TypeError: Cannot read property 'text' of undefined
    at Proxy.render (eval at ./node_modules/cache-loader/dist/cjs.js?{"cacheDirectory":"node_modules/.cache/vue-loader","cacheIdentifier":"db619a62-vue-loader-template"}!./node_modules/vue-loader/lib/loaders/templateLoader.js?!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/components/mainComp/subComp/ExperienceText.vue?vue&type=template&id=088f3b1e& (app.js:1089), <anonymous>:15:36)
    at VueComponent.Vue._render (vue.runtime.esm.js?2b0e:3548)
    at VueComponent.updateComponent (vue.runtime.esm.js?2b0e:4066)
    at Watcher.get (vue.runtime.esm.js?2b0e:4479)
    at new Watcher (vue.runtime.esm.js?2b0e:4468)
    at mountComponent (vue.runtime.esm.js?2b0e:4073)
    at VueComponent.Vue.$mount (vue.runtime.esm.js?2b0e:8415)
    at init (vue.runtime.esm.js?2b0e:3118)
    at createComponent (vue.runtime.esm.js?2b0e:5978)
    at createElm (vue.runtime.esm.js?2b0e:5925)

The JSON arrived

Proof of json data reached.

In my app, the article tag is not visible at all. I am pretty sure it is because the JSON data is not being rendered properly. How do i fix this issue?


Solution

  • First, remove the : from v-for, since it's a directive and not a binding.

    <article class="content__box" v-for="data in expJson">
    

    You also want a :key on all v-for elements. Since your data doesn't have a unique identifier, we can use the index:

    <article class="content__box" v-for="(data, index) in expJson" :key="index">
    

    Additionally, this still gives an error because all components must have one root element and v-for can create potentially multiple root elements. You can wrap everything in a div:

    <template>
      <div>
        <article class="content__box" v-for="(data, index) in expJson" :key="index">
          {{data.text}}
          <h6>{{data.text}}</h6>
          <h3>{{data.color}}</h3>
          <p>{{data.size}}</p>
        </article>
      </div>
    </template>