Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

uniqueginun's avatar

Inertia.js / Vue Error

Hello

I'm learning inertia and I'm creating my first app. I'm creating a simple component that read-write tweets and display them.

Tweets/Index.vue

<template>
    <app-layout>
        <template #header>
            <h2 class="h4 font-weight-bold">Tweets</h2>
        </template>

        <div class="row">
            <div class="col-md-8">
                <div
                    class="py-4 d-flex align-items-center justify-content-between"
                >
                    <input
                        type="text"
                        class="mr-2 form-control"
                        v-model="tweetForm.body"
                        ref="tweetBox"
                    />
                    <jet-button @click="sendTweet">Tweet</jet-button>
                   
                </div>
                <div class="mt-4">
                    <div
                        v-if="tweets.length"
                        v-for="tweet in tweets"
                        :key="tweet.id"
                        class="px-3 py-2 mb-3 border rounded bg-dark"
                    >
                        <div class="mb-2 text-light">{{ tweet.body }}</div>
                        <div
                            class="d-flex align-items-center justify-content-between"
                        >
                            <span class="text-sm-left text-muted"
                                >by @{{ tweet.creator }}</span
                            >
                            <div class="text-sm-left text-muted">
                                {{ tweet.published_at }}
                            </div>
                        </div>
                    </div>

                    <div v-else>
                            <h6 class="text-sm-left text-muted">
                                You Haven't tweeted yet?
                                <a
                                    class="text-decoration-none"
                                    @click.prevent="$refs.tweetBox.focus"
                                    href="#"
                                    >share your first thought</a
                                >
                            </h6>
                        </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="py-4">
                    <h4 class="text-center">Trendings</h4>
                </div>
            </div>
        </div>
    </app-layout>
</template>

<script>
import AppLayout from "@/Layouts/AppLayout";
import JetButton from "@/Jetstream/Button";

export default {
    props: ["tweets"],

    data() {
        return {
            tweetForm: this.$inertia.form({
                body: ""
            })
        };
    },

    methods: {
        sendTweet() {
            this.tweetForm.post(route("tweets.store"), {
                errorBag: "sendTweet",
                preserveScroll: true,
                onSuccess: () => this.tweetForm.reset()
            });
        }
    },

    components: {
        AppLayout,
        JetButton
    }
};
</script>

controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Inertia\Inertia;

class TweetsController extends Controller
{
    public function index(Request $request)
    {

        return Inertia::render('Tweets/Index', [
            'tweets' => $request->user()->tweets
        ]);
    }

    public function store(Request $request)
    {
        $valid = $this->validate($request, [
            'body' => 'required|max:150'
        ]);

        $request->user()->tweets()->create($valid);

        return Redirect::route('tweets.index');
    }
}

Now when I create new tweet it should re-render the component and display new tweet in no time. but I get this error ncaught (in promise) TypeError: Cannot read property 'insertBefore' of null which basically is here:

insert: (child, parent, anchor) => {
        parent.insertBefore(child, anchor || null);
    },

what is wrong with my code?

0 likes
9 replies
uniqueginun's avatar
uniqueginun
OP
Best Answer
Level 9

solved! because the @ symbol I forgot about that

1 like
Manouher's avatar

same problem, which @ symbol should be deleted

FutureWeb's avatar

in my case its something to do with template removing the app layout fixes the issue

<template>
             <app-layout> <!--- removing this works -->
                    <div>rest of template</div>
              </app-layout> 
       </template>

then import your layout in the script tags

<script>
import AppLayout from '@/Layouts/AppLayout'

export default {
   components: {
        AppLayout,
    },
    layout: AppLayout,
}
jjoek's avatar

Just had a similar problem, in my case after a while this came down to v-if instead of v-show. I concluded that maybe from vues' functionality on v-if where the given element is actually part of the end html but rather only shown when necessary but this is only happening in vue3 as i think I have worked with such on vue2 previously.

my browser console problem is or was: (for reference cases maybe)

    app.js:12900 Uncaught (in promise) TypeError: Cannot read property 'insertBefore' of null
    at insert (app.js:12900)
    at mountElement (app.js:9615)
    at processElement (app.js:9556)
    at patch (app.js:9476)
    at patchKeyedChildren (app.js:10326)
    at patchChildren (app.js:10128)

And this happened when displaying a form errors from inertia form object

grantholle's avatar

I'm having this issue but only on a production build. I have v-if and no v-show and it's only happening on one page with one request. Hmm, very weird error with no way to pinpoint it for me, as I have to build production each time to replicate it

Please or to participate in this conversation.