0

I have this code in my VUE file:

<template>
<div class="row">
    <div class="col-12">
        <section class="list">
            <draggable class="drag-area" :list="picsNew" :options="{animation:200, group:'status'}" :element="'article'" @add="onAdd($event, false)"  @change="update">
                <article class="card" v-for="(photo, index) in picsNew" :key="photo.id" :data-id="photo.id">
                    <header>
                        {{ this.galCode }}{{ photo.filename }}
                    </header>
                </article>
            </draggable>
        </section>
    </div>
</div>
</template>

<script>
import draggable from 'vuedraggable'
export default {
    components: {
        draggable
    },

    props: ['myPics', 'galId', 'phCode', 'galCode'],


    data() {
        return {
            picsNew: this.myPics,
        }
    },
    methods: {

        update() {
            this.picsNew.map((photo, index) => {
                photo.order = index + 1;
            });


            let photos = this.picsNew;

            console.log(this.galCode)
            axios.put('/gallery/' + this.galId + '/updateAll', {
                photos: photos
            }).then((response) => {
                console.log(response.data);
            }).catch((error) => {
                console.log(error);
            })
        }

    }
}
</script>

in the template, photo.filename works, but this.galCode throws these two errors:

app.js:44152 [Vue warn]: Error in render: "TypeError: Cannot read property 'galCode' of undefined"

found in

 ---> <DraggablePic> at resources/js/components/draggablepic.vue

app.js:45415 TypeError: Cannot read property 'galCode' of undefined

The variable contains a value, as i am printing it to console. What am I doing wrong?

3 Answers 3

2

The line {{ this.galCode }}{{ photo.filename }} should be {{ galCode }}{{ photo.filename }}. Your problem is the this you added

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

Comments

1

The problem is that you try to access props variable directly from your template. To solve this, initalize a state variable (under data {}) Within props value as default like you did With picsNeW. another remark, avoid to use "this" from template, you should access directly datas form its name. Use default value in your props, this is recommended :)

1 Comment

you mean like the picsNew variable?
1

First you should define Default value as :

  props: {
    name: {
      type: String,
      default: 'default'
    },
    ...
    galCode: {
      type: Number,
      default: 0
    },
  },

Second be sure you receive desired data, specially in nested object you should check the object defined to prevent receive undefined property error, like use v-if :

<header>
    <template v-if="galCode">{{ galCode }}</template>    // can use v-else here too print default value
    <template v-if="photo.filename"> {{ photo.filename }}</template>

</header>

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.