Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
jquery.dataTables.js: Cannot read properties of undefined with Svelte

I use svelte 3 with datatables.js and jquery.js in browser only (not ssr).

After datatables make ajax request and try to create table I got error:

Uncaught TypeError: Cannot read properties of undefined (reading 'createElement')
    at _fnCreateTr (jquery.dataTables.js:3160)

In firefox error was: Uncaught TypeError: document is undefined .

With chrome debuger I see that document == undefined .

How is posible that document is not defined in browser?

My code is:

<script lang="ts">
    import {onMount, tick} from "svelte";

    import jQuery from 'jquery'
    import dt from 'datatables.net-dt'

    import {flatten, SITE_URL} from "../config";
    import fixJquery from "../includes/jquery.spring-friendly";


    dt(jQuery)


    function handleError(xhr, error, thrown) {
        console.error("ERROR", xhr.responseText);
    }

    fixJquery(jQuery);
    onMount(async () => {
        await tick();

        jQuery(document).ready( function () {

            let dataTable = jQuery("#example").DataTable(
                {
                    processing: true,
                    serverSide: true,
                    ajax: {
                        url: SITE_URL + "/users/datatables",
                        type: "GET",
                        error: handleError
                    },
                    columns: [
                        {
                            data: 'id'
                        },
                        {
                            data: 'name'
                        }
                    ],
                }
            );
        });
    });


</script>

<div>
    <h2>User list</h2>

    <table id="example" class="display" style="width:100%">
        <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
        </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</div>



Here is Svelte REPL with this problem: svelte.dev/repl/8354f4daadc04165a59dbac5be41e253?version=3.44.2

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Depending on how you load DataTables library, it may see a different scope of the world and differently initialised jQuery. I encountered this issue before. Thus, it is better to explicitly initialise DataTables and pass jQuery and its document to it.

Here is some of our open source code that is using Svelte + DataTables (+ its jQuery dependency)

You can see the live website here using Svelte + DataTables.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda