vue3 Cannot read property 'insertBefore' of null
Asked Answered
F

3

6

I'm getting this error when i try to update my data fields in vuejs.

data() {
    return {
        form : useForm({
            imageFile : null,
            img_id : null,
        }),
        product_images : this.images,
    }
},

here I'm updating the product_images after the success of post request sent by the user.

like this.

this.form.post(url, {
    onStart : () => {
        Inertia.on('progress', (event) => {
            if (event.detail.progress.percentage) {
                NProgress.set((event.detail.progress.percentage / 100) * 0.98);
            }
        });
    },
    onFinish : () => {
        this.product_images = this.images;
    },
})

showing the product_images in template be like

<div v-for="image in product_images" class="col-xl-4 col-lg-6 col-12 mb-3" :key="image.id">
     <input-image @changeImage="onChangeImage" :id="image.id" :image="image.image" />
</div>

when product_images changes after the post request, product_images not update in DOM but get this error why?

app.js:12889 Uncaught (in promise) TypeError: Cannot read property 'insertBefore' of null
    at insert (app.js:12889)
    at mountElement (app.js:9604)
    at processElement (app.js:9545)
    at patch (app.js:9465)
    at patchKeyedChildren (app.js:10315)
    at patchChildren (app.js:10093)
    at processFragment (app.js:9839)
    at patch (app.js:9461)
    at patchKeyedChildren (app.js:10174)
    at patchChildren (app.js:10117)
Favian answered 6/6, 2021 at 17:46 Comment(0)
U
6

Unfortunately the vue error message is not very helpful.

In my case, I had some uninitialized data in the html template like this:

<template>
    <div>
        {{ data.xyz }}
    </div>
</template>

Change this to either:

<template v-if="!!data">
    <div>
        {{ data.xyz }}
    </div>
</template>

Or:

<template>
    <div>
        {{ data?.xyz }}
    </div>
</template>
Undulatory answered 22/6, 2021 at 21:5 Comment(2)
i solve it using another method. i just change my entire structure of code to get rid of this error.Favian
@Tobias Ernst first I ignore your answer because I founded another solution for the previous error but today I got the same error and tried your method man and it's worked. You are a life saviour man. I wish I could hug you. For me, it's like you save my life.Favian
J
1

I fixed it by updating my Vue to the latest version ([email protected]).

Joey answered 28/10, 2021 at 9:26 Comment(0)
R
1

In my case, was mistakenly calling app.mount('#app') twice.

Revanchism answered 3/11, 2022 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.