1

I am trying to use a component named CardRenderer.vue which renders card using array of Objects. I am using the same component again & again to render the data. I am having this error "[Vue warn]: Invalid prop: type check failed for prop "renderData" when I tried passing prop from component.

I tried passing different values and different types but it did'nt work.

Here is the code:

CardRenderer.vue

<template lang="html">

  <div>       
    <b-container class="bv-example-row">      
       <b-row v-for="(row, i) of rows" v-bind:key="i">       
          <b-col v-for="(item, j) of row" v-bind:key="j" >

                    <!-- you card -->
              <b-card 
                :title="item.title" 
                img-src="item.icon" 
                img-alt="Image" 
                img-top 
                tag="article" 
                style="max-width: 20rem;" 
                class="mb-2"
              >
                <b-card-text>
                  <h1>{{item.name}}</h1>
                  <pre>{{item.description}}</pre>
                </b-card-text>
                  <b-button :href="'/dashboard/'+item.name" variant="primary">More</b-button>
              </b-card>                
          </b-col>
        </b-row>
    </b-container>    
  </div>

</template>

<script lang="js">
  export default  {
    name: 'CardRenderer',
    props: {
      renderData: []
    },
     data() {
      return {
        rows: null
      }
    },
    mounted() {

      const itemsPerRow = 3
      let rowss = []
      let arr = this.renderData
      // eslint-disable-next-line
      // console.log(this.renderData)
      for (let i = 0; i < arr.length; i += itemsPerRow){
          let row = []
          for (let z = 0; z < itemsPerRow; z++) {
            row.push(arr[z])
          }
          rowss.push(row)
      }


      this.rows = rowss

      // eslint-disable-next-line                
      console.log(this.rows) 

    },

    methods: {

    },
    computed: {
      //  rows() {
      //  }

    }
  }
</script>

<style scoped>

</style>

CardGrouper.vue:

<template lang="html">

  <div  class = "full" >

    <div class="h-50" style=" background-color: #C8544F">
      <h1 align="center">{{$store.getters.responseAPI.title}} </h1>

      <CardRenderer :renderData=this.$store.getters.responseAPI.apps />
    </div>

  </div>

</template>

<script>
import CardRenderer from "./CardRenderer.vue"
/* eslint-disable */
  export default  {
    name: 'CardGrouper',
    components: {
      CardRenderer
    },
    props: [],
    mounted() {

    },
    data() {
      return {

      }
    },
    methods: {

    },
    computed: {

    }
}
</script>

<style scoped >
  .full{
    width: 100vw;
    height: 90vh;
    background: linear-gradient(to bottom, Red 30%, white 50%);
}
</style>

Something.vue

<template lang="html">
    <!-- <h1>Something</h1> -->
    <CardRenderer :renderData=valObj />
</template>

<script lang="js">
import CardRenderer from './CardRenderer'

    export default  {
        name: 'something',
        components: {
            CardRenderer
        },
        props: [],

        data() {
            return {
                valObj: []
            }
        },
        mounted() {
            let key = this.findUrl()
            let value = this.$store.getters.responseAPI.apps.filter((elem) => {
                if(elem.name == key) return elem.apps
            })

            if (value.length > 0)
                this.valObj = value[0].apps
            //eslint-disable-next-line
            console.log(this.valObj)
        },
        methods: {
            findUrl() {
                let url  = window.location.pathname.split("/").slice(-1)[0];
                return url
            }       
        },
        computed: {

        }
    }
</script>

<style scoped >
  .something {

  }
</style>

I am having this error. It looks like this. enter image description here

This is the data I am passing as a prop from Something.vue enter image description here

This is how value looks like enter image description here

Error is being generated somewhere from Something.vue. I am passing array of objects as a prop. How do i rectify this error, to make it work.

3 Answers 3

3

Set the renderData type as Array and default value to []:

props: {
  renderData: {
    type: Array,
    default: () => []
  }
}
Sign up to request clarification or add additional context in comments.

10 Comments

It worked.. But, I am having another error. When i access the prop it shows undefined
From where you are getting undefined? Default value is [] so, it should not be.
"Error in mounted hook: "TypeError: Cannot read property 'length' of undefined" This is the error it shows. Seems like its somewhere coming from CardRenderer.vue in the mounted hook
Look for the data I am passing in through Something.vue. I just added the screenshot of it
What is the value of this.$store.getters.responseAPI.apps?
|
1

It appears that you are defining your renderData prop as an array [] but probably are passing an object to it or something.

Either simplify it and do...

props: ['renderData']

Or if you are passing an object to it do..

    props: {
         renderData: {
            type: Object,
          }
     }

If it is an array of objects do..

 props: {
     renderData: {
        type: Array,
        default: () => [{}];
      }

1 Comment

I want to pass an array of objects and thats what i am passing... i think! :p
0

just for doc.

    // Object with a default value
propE: {
  type: Object,
  // Object or array defaults must be returned from
  // a factory function
  default: function () {
    return { message: 'hello' }
  }
},

This is in vue prop documentation

Comments

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.