Error in render: TypeError: file is undefined












1














I'm fairly new to Vue and don't fully understand the error I put as the title. I've read other posts about this error and haven't been able to figure out how to fix this issue for my project. I'm trying to make a file uploader that is written in Vue and interacts with Laravel on the backend. I understand that this property is undefined, however it seems fine before I attempt to upload a file. The files actually upload just fine in the backend, but after it uploads I get this error in the console when it's trying to display:




[Vue warn]: Error in render: "TypeError: file is undefined"

found in
---> at resources/js/components/ActionLogComponent.vue




Here's the relevant code:



<div class="form-group filezone">
<input type="file" id="files" ref="files" multiple v-on:change="handleFiles()" />
<p>Drop files here <br>or click to search.</p>

<div v-for="(file, key) in files" class="file-listing">
<img class="preview" v-bind:ref="'preview' + parseInt(key)" /> {{ file.name }}
<div class="success-container" v-if="file.id > 0">
Success
<input type="hidden" :name="input_name" :value="file.id" />
</div>
<div class="remove-container" v-else>
<a class="remove" v-on:click="removeFile(key)">Remove</a>
</div>
</div>

<a class="submit-button" v-on:click="submitFiles()" v-show="files.length > 0">Submit</a>
</div>


And here is the relevant part of the export from the vue file



export default {
props: ['companyName', 'userFullName', 'input_name', 'post_url'],
data() {
return {
actions: ,
files: ,
viewing: '',
csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
createAction: {
errors: ,
date: '',
company: '',
name: '',
communication_type: '',
contact: '',
current_status: '',
action_item: ''
},
archiveAction: {
id: ''
},
url: ''
}
},


I have this method in my methods part that handles the file upload.



submitFiles() {
for(let i = 0; i < this.files.length; i++) {
if(this.files[i].id) {
continue;
}

//create form data to send
let formData = new FormData();

formData.append('file', this.files[i]);
formData.append('viewing', this.viewing);

axios.post('/upload-product-file',
formData,
{
headers: {
'Content-Type': 'multipart/form-data'
}
})
.then(function(data) {
this.files[i].id = data['data'].id;
this.files.splice(i, 1, this.files['id']);
console.log('success');
}.bind(this))
.catch(function(error) {
console.log(error);
});

console.log('Just uploaded image. Files array should come next.');
console.log(this.files);
}
}


UPDATE
Code for handleFiles():



handleFiles() {
let uploadedFiles = this.$refs.files.files;

for(var i = 0; i < uploadedFiles.length; i++) {
this.files.push(uploadedFiles[i]);
}

this.getImagePreviews();
},


Thank you in advance for any help or insight on this!










share|improve this question
























  • Please, add relevant code for handleFiles as well.
    – aBiscuit
    Nov 15 at 20:01










  • Hi, no problem, just updated with the code. Thank you!
    – Frank A.
    Nov 15 at 22:11
















1














I'm fairly new to Vue and don't fully understand the error I put as the title. I've read other posts about this error and haven't been able to figure out how to fix this issue for my project. I'm trying to make a file uploader that is written in Vue and interacts with Laravel on the backend. I understand that this property is undefined, however it seems fine before I attempt to upload a file. The files actually upload just fine in the backend, but after it uploads I get this error in the console when it's trying to display:




[Vue warn]: Error in render: "TypeError: file is undefined"

found in
---> at resources/js/components/ActionLogComponent.vue




Here's the relevant code:



<div class="form-group filezone">
<input type="file" id="files" ref="files" multiple v-on:change="handleFiles()" />
<p>Drop files here <br>or click to search.</p>

<div v-for="(file, key) in files" class="file-listing">
<img class="preview" v-bind:ref="'preview' + parseInt(key)" /> {{ file.name }}
<div class="success-container" v-if="file.id > 0">
Success
<input type="hidden" :name="input_name" :value="file.id" />
</div>
<div class="remove-container" v-else>
<a class="remove" v-on:click="removeFile(key)">Remove</a>
</div>
</div>

<a class="submit-button" v-on:click="submitFiles()" v-show="files.length > 0">Submit</a>
</div>


And here is the relevant part of the export from the vue file



export default {
props: ['companyName', 'userFullName', 'input_name', 'post_url'],
data() {
return {
actions: ,
files: ,
viewing: '',
csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
createAction: {
errors: ,
date: '',
company: '',
name: '',
communication_type: '',
contact: '',
current_status: '',
action_item: ''
},
archiveAction: {
id: ''
},
url: ''
}
},


I have this method in my methods part that handles the file upload.



submitFiles() {
for(let i = 0; i < this.files.length; i++) {
if(this.files[i].id) {
continue;
}

//create form data to send
let formData = new FormData();

formData.append('file', this.files[i]);
formData.append('viewing', this.viewing);

axios.post('/upload-product-file',
formData,
{
headers: {
'Content-Type': 'multipart/form-data'
}
})
.then(function(data) {
this.files[i].id = data['data'].id;
this.files.splice(i, 1, this.files['id']);
console.log('success');
}.bind(this))
.catch(function(error) {
console.log(error);
});

console.log('Just uploaded image. Files array should come next.');
console.log(this.files);
}
}


UPDATE
Code for handleFiles():



handleFiles() {
let uploadedFiles = this.$refs.files.files;

for(var i = 0; i < uploadedFiles.length; i++) {
this.files.push(uploadedFiles[i]);
}

this.getImagePreviews();
},


Thank you in advance for any help or insight on this!










share|improve this question
























  • Please, add relevant code for handleFiles as well.
    – aBiscuit
    Nov 15 at 20:01










  • Hi, no problem, just updated with the code. Thank you!
    – Frank A.
    Nov 15 at 22:11














1












1








1







I'm fairly new to Vue and don't fully understand the error I put as the title. I've read other posts about this error and haven't been able to figure out how to fix this issue for my project. I'm trying to make a file uploader that is written in Vue and interacts with Laravel on the backend. I understand that this property is undefined, however it seems fine before I attempt to upload a file. The files actually upload just fine in the backend, but after it uploads I get this error in the console when it's trying to display:




[Vue warn]: Error in render: "TypeError: file is undefined"

found in
---> at resources/js/components/ActionLogComponent.vue




Here's the relevant code:



<div class="form-group filezone">
<input type="file" id="files" ref="files" multiple v-on:change="handleFiles()" />
<p>Drop files here <br>or click to search.</p>

<div v-for="(file, key) in files" class="file-listing">
<img class="preview" v-bind:ref="'preview' + parseInt(key)" /> {{ file.name }}
<div class="success-container" v-if="file.id > 0">
Success
<input type="hidden" :name="input_name" :value="file.id" />
</div>
<div class="remove-container" v-else>
<a class="remove" v-on:click="removeFile(key)">Remove</a>
</div>
</div>

<a class="submit-button" v-on:click="submitFiles()" v-show="files.length > 0">Submit</a>
</div>


And here is the relevant part of the export from the vue file



export default {
props: ['companyName', 'userFullName', 'input_name', 'post_url'],
data() {
return {
actions: ,
files: ,
viewing: '',
csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
createAction: {
errors: ,
date: '',
company: '',
name: '',
communication_type: '',
contact: '',
current_status: '',
action_item: ''
},
archiveAction: {
id: ''
},
url: ''
}
},


I have this method in my methods part that handles the file upload.



submitFiles() {
for(let i = 0; i < this.files.length; i++) {
if(this.files[i].id) {
continue;
}

//create form data to send
let formData = new FormData();

formData.append('file', this.files[i]);
formData.append('viewing', this.viewing);

axios.post('/upload-product-file',
formData,
{
headers: {
'Content-Type': 'multipart/form-data'
}
})
.then(function(data) {
this.files[i].id = data['data'].id;
this.files.splice(i, 1, this.files['id']);
console.log('success');
}.bind(this))
.catch(function(error) {
console.log(error);
});

console.log('Just uploaded image. Files array should come next.');
console.log(this.files);
}
}


UPDATE
Code for handleFiles():



handleFiles() {
let uploadedFiles = this.$refs.files.files;

for(var i = 0; i < uploadedFiles.length; i++) {
this.files.push(uploadedFiles[i]);
}

this.getImagePreviews();
},


Thank you in advance for any help or insight on this!










share|improve this question















I'm fairly new to Vue and don't fully understand the error I put as the title. I've read other posts about this error and haven't been able to figure out how to fix this issue for my project. I'm trying to make a file uploader that is written in Vue and interacts with Laravel on the backend. I understand that this property is undefined, however it seems fine before I attempt to upload a file. The files actually upload just fine in the backend, but after it uploads I get this error in the console when it's trying to display:




[Vue warn]: Error in render: "TypeError: file is undefined"

found in
---> at resources/js/components/ActionLogComponent.vue




Here's the relevant code:



<div class="form-group filezone">
<input type="file" id="files" ref="files" multiple v-on:change="handleFiles()" />
<p>Drop files here <br>or click to search.</p>

<div v-for="(file, key) in files" class="file-listing">
<img class="preview" v-bind:ref="'preview' + parseInt(key)" /> {{ file.name }}
<div class="success-container" v-if="file.id > 0">
Success
<input type="hidden" :name="input_name" :value="file.id" />
</div>
<div class="remove-container" v-else>
<a class="remove" v-on:click="removeFile(key)">Remove</a>
</div>
</div>

<a class="submit-button" v-on:click="submitFiles()" v-show="files.length > 0">Submit</a>
</div>


And here is the relevant part of the export from the vue file



export default {
props: ['companyName', 'userFullName', 'input_name', 'post_url'],
data() {
return {
actions: ,
files: ,
viewing: '',
csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
createAction: {
errors: ,
date: '',
company: '',
name: '',
communication_type: '',
contact: '',
current_status: '',
action_item: ''
},
archiveAction: {
id: ''
},
url: ''
}
},


I have this method in my methods part that handles the file upload.



submitFiles() {
for(let i = 0; i < this.files.length; i++) {
if(this.files[i].id) {
continue;
}

//create form data to send
let formData = new FormData();

formData.append('file', this.files[i]);
formData.append('viewing', this.viewing);

axios.post('/upload-product-file',
formData,
{
headers: {
'Content-Type': 'multipart/form-data'
}
})
.then(function(data) {
this.files[i].id = data['data'].id;
this.files.splice(i, 1, this.files['id']);
console.log('success');
}.bind(this))
.catch(function(error) {
console.log(error);
});

console.log('Just uploaded image. Files array should come next.');
console.log(this.files);
}
}


UPDATE
Code for handleFiles():



handleFiles() {
let uploadedFiles = this.$refs.files.files;

for(var i = 0; i < uploadedFiles.length; i++) {
this.files.push(uploadedFiles[i]);
}

this.getImagePreviews();
},


Thank you in advance for any help or insight on this!







javascript laravel vue.js vuejs2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 22:10

























asked Nov 15 at 19:25









Frank A.

6581611




6581611












  • Please, add relevant code for handleFiles as well.
    – aBiscuit
    Nov 15 at 20:01










  • Hi, no problem, just updated with the code. Thank you!
    – Frank A.
    Nov 15 at 22:11


















  • Please, add relevant code for handleFiles as well.
    – aBiscuit
    Nov 15 at 20:01










  • Hi, no problem, just updated with the code. Thank you!
    – Frank A.
    Nov 15 at 22:11
















Please, add relevant code for handleFiles as well.
– aBiscuit
Nov 15 at 20:01




Please, add relevant code for handleFiles as well.
– aBiscuit
Nov 15 at 20:01












Hi, no problem, just updated with the code. Thank you!
– Frank A.
Nov 15 at 22:11




Hi, no problem, just updated with the code. Thank you!
– Frank A.
Nov 15 at 22:11












2 Answers
2






active

oldest

votes


















0














Try adding file to your data export



data() {
return {
actions: ,
file: '',
files: ,
viewing: '',


I think is because you trying to access the file while files are null so technically you never actually loop through the files so you are not creating the file object.
The other thing is you can use in the div I would check to if files !== null that way it avoids giving you any errors. meaning if the files are null it does't try to render the rest of what's inside the div, avoiding the errors.



Here is a good read on this
https://medium.com/devmarketer/how-to-add-conditional-statements-to-v-for-loops-in-vue-js-c0b4d17e7dfd



I hope this helps






share|improve this answer





















  • Hey, thanks for the response! I read the article and tried putting the property file into data and checked for null, but unfortunately I keep getting the same error. I guess since I'm inexperienced with vue, I'm not completely sure what else might be relevant to the issue.
    – Frank A.
    Nov 15 at 22:09



















0














I spent most of the day banging my head against the wall with this problem and since I was following a tutorial to help me build this I decided to go through the code again, line by line. It turns out in the submitFiles method there's this line:



this.files.splice(i, 1, this.files['id']);


which actually removes one element from the files array. I'm not certain if the author of the tutorial had a specific intention for this line of code or not, but they didn't explain what it was for. What I realized was happening though is it removes one element of the array each iteration of the for loop, which at some point empties the entire array. When I commented this line out, I no longer get the error. I don't know that this is actually the correct answer to my problem, but for now I'll just run with it since Vue no longer complains.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326627%2ferror-in-render-typeerror-file-is-undefined%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Try adding file to your data export



    data() {
    return {
    actions: ,
    file: '',
    files: ,
    viewing: '',


    I think is because you trying to access the file while files are null so technically you never actually loop through the files so you are not creating the file object.
    The other thing is you can use in the div I would check to if files !== null that way it avoids giving you any errors. meaning if the files are null it does't try to render the rest of what's inside the div, avoiding the errors.



    Here is a good read on this
    https://medium.com/devmarketer/how-to-add-conditional-statements-to-v-for-loops-in-vue-js-c0b4d17e7dfd



    I hope this helps






    share|improve this answer





















    • Hey, thanks for the response! I read the article and tried putting the property file into data and checked for null, but unfortunately I keep getting the same error. I guess since I'm inexperienced with vue, I'm not completely sure what else might be relevant to the issue.
      – Frank A.
      Nov 15 at 22:09
















    0














    Try adding file to your data export



    data() {
    return {
    actions: ,
    file: '',
    files: ,
    viewing: '',


    I think is because you trying to access the file while files are null so technically you never actually loop through the files so you are not creating the file object.
    The other thing is you can use in the div I would check to if files !== null that way it avoids giving you any errors. meaning if the files are null it does't try to render the rest of what's inside the div, avoiding the errors.



    Here is a good read on this
    https://medium.com/devmarketer/how-to-add-conditional-statements-to-v-for-loops-in-vue-js-c0b4d17e7dfd



    I hope this helps






    share|improve this answer





















    • Hey, thanks for the response! I read the article and tried putting the property file into data and checked for null, but unfortunately I keep getting the same error. I guess since I'm inexperienced with vue, I'm not completely sure what else might be relevant to the issue.
      – Frank A.
      Nov 15 at 22:09














    0












    0








    0






    Try adding file to your data export



    data() {
    return {
    actions: ,
    file: '',
    files: ,
    viewing: '',


    I think is because you trying to access the file while files are null so technically you never actually loop through the files so you are not creating the file object.
    The other thing is you can use in the div I would check to if files !== null that way it avoids giving you any errors. meaning if the files are null it does't try to render the rest of what's inside the div, avoiding the errors.



    Here is a good read on this
    https://medium.com/devmarketer/how-to-add-conditional-statements-to-v-for-loops-in-vue-js-c0b4d17e7dfd



    I hope this helps






    share|improve this answer












    Try adding file to your data export



    data() {
    return {
    actions: ,
    file: '',
    files: ,
    viewing: '',


    I think is because you trying to access the file while files are null so technically you never actually loop through the files so you are not creating the file object.
    The other thing is you can use in the div I would check to if files !== null that way it avoids giving you any errors. meaning if the files are null it does't try to render the rest of what's inside the div, avoiding the errors.



    Here is a good read on this
    https://medium.com/devmarketer/how-to-add-conditional-statements-to-v-for-loops-in-vue-js-c0b4d17e7dfd



    I hope this helps







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 at 20:10









    Linuxuser07

    214




    214












    • Hey, thanks for the response! I read the article and tried putting the property file into data and checked for null, but unfortunately I keep getting the same error. I guess since I'm inexperienced with vue, I'm not completely sure what else might be relevant to the issue.
      – Frank A.
      Nov 15 at 22:09


















    • Hey, thanks for the response! I read the article and tried putting the property file into data and checked for null, but unfortunately I keep getting the same error. I guess since I'm inexperienced with vue, I'm not completely sure what else might be relevant to the issue.
      – Frank A.
      Nov 15 at 22:09
















    Hey, thanks for the response! I read the article and tried putting the property file into data and checked for null, but unfortunately I keep getting the same error. I guess since I'm inexperienced with vue, I'm not completely sure what else might be relevant to the issue.
    – Frank A.
    Nov 15 at 22:09




    Hey, thanks for the response! I read the article and tried putting the property file into data and checked for null, but unfortunately I keep getting the same error. I guess since I'm inexperienced with vue, I'm not completely sure what else might be relevant to the issue.
    – Frank A.
    Nov 15 at 22:09













    0














    I spent most of the day banging my head against the wall with this problem and since I was following a tutorial to help me build this I decided to go through the code again, line by line. It turns out in the submitFiles method there's this line:



    this.files.splice(i, 1, this.files['id']);


    which actually removes one element from the files array. I'm not certain if the author of the tutorial had a specific intention for this line of code or not, but they didn't explain what it was for. What I realized was happening though is it removes one element of the array each iteration of the for loop, which at some point empties the entire array. When I commented this line out, I no longer get the error. I don't know that this is actually the correct answer to my problem, but for now I'll just run with it since Vue no longer complains.






    share|improve this answer


























      0














      I spent most of the day banging my head against the wall with this problem and since I was following a tutorial to help me build this I decided to go through the code again, line by line. It turns out in the submitFiles method there's this line:



      this.files.splice(i, 1, this.files['id']);


      which actually removes one element from the files array. I'm not certain if the author of the tutorial had a specific intention for this line of code or not, but they didn't explain what it was for. What I realized was happening though is it removes one element of the array each iteration of the for loop, which at some point empties the entire array. When I commented this line out, I no longer get the error. I don't know that this is actually the correct answer to my problem, but for now I'll just run with it since Vue no longer complains.






      share|improve this answer
























        0












        0








        0






        I spent most of the day banging my head against the wall with this problem and since I was following a tutorial to help me build this I decided to go through the code again, line by line. It turns out in the submitFiles method there's this line:



        this.files.splice(i, 1, this.files['id']);


        which actually removes one element from the files array. I'm not certain if the author of the tutorial had a specific intention for this line of code or not, but they didn't explain what it was for. What I realized was happening though is it removes one element of the array each iteration of the for loop, which at some point empties the entire array. When I commented this line out, I no longer get the error. I don't know that this is actually the correct answer to my problem, but for now I'll just run with it since Vue no longer complains.






        share|improve this answer












        I spent most of the day banging my head against the wall with this problem and since I was following a tutorial to help me build this I decided to go through the code again, line by line. It turns out in the submitFiles method there's this line:



        this.files.splice(i, 1, this.files['id']);


        which actually removes one element from the files array. I'm not certain if the author of the tutorial had a specific intention for this line of code or not, but they didn't explain what it was for. What I realized was happening though is it removes one element of the array each iteration of the for loop, which at some point empties the entire array. When I commented this line out, I no longer get the error. I don't know that this is actually the correct answer to my problem, but for now I'll just run with it since Vue no longer complains.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 at 0:49









        Frank A.

        6581611




        6581611






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326627%2ferror-in-render-typeerror-file-is-undefined%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?