Uncaught Syntaxerror: Unexpected token {

Asked

Viewed 2,091 times

3

I was developing this code and the following error was generated:

Uncaught Syntaxerror: Unexpected token {

$(document).ready(function() {
        editor = new $.fn.dataTable.Editor( {
            ajax: "../php/staff.php",
            table: "#publicationTable",
            fields: [ {
                    label: "Visualizado:",
                    name: "p.id_Publication"
                }, {
                    label: "Título da Publicação:",
                    name: "ti.PublicationTitle"
                }, {
                    label: "Tipo de Publicação:",
                    name: "ty.PublicationType"
                }, {
                    label: "Ano:",
                    name: "p.ano"
                }, {
                    label: "Mês:",
                    name: "p.competencia"
                }, {
                    label: "Empresa:",
                    name: "c.razaoSocial"
                }, {
                    label: "Favorecido:",
                    name: "e.nome"
                }
            ]
        } );

        var table = $('#publicationTable').DataTable( {
            lengthChange: true,
            ajax: "../php/staff.php",
            columns: [
                { data: "p.status",
                render: function ( data, type, row ) {
                    var text = "";
                    if (type == "display") {
                        if (data == "1") {
                            text = "<i class='ace-icon fa fa-circle green'></i>";
                        } else {
                            text = "<i class='ace-icon fa fa-circle red'></i>";
                        }
                        data = text
                    }
                    return data;
                },
            }
                { data: "ti.PublicationTitle" },
                { data: "ty.PublicationType" },
                { data: "p.ano" },
                { data: "p.competencia" },
                { data: "c.razaoSocial" },
                { data: "e.nome" }
            ],
        } );
    } );

I would like to know why this mistake is occurring, and how can I resolve it?

  • The two answers are working, it wasn’t just one to work?

  • I voted for the other because it was the one that answered first

2 answers

4

The line 48 is this:

                return data;
            },
        } < ----------------- LINHA 48 AQUI
            { data: "ti.PublicationTitle" },
            { data: "ty.PublicationType" },
            { data: "p.ano" },
            { data: "p.competencia" },
            { data: "c.razaoSocial" },
            { data: "e.nome" }
        ],

See that columns has a array, for each {data:...} must have a comma to separate, so it’s missing before { data: "ti.PublicationTitle" } the comma to separate from:

    { data: "p.status",
    render: function ( data, type, row ) {
        var text = "";
        if (type == "display") {
            if (data == "1") {
                text = "<i class='ace-icon fa fa-circle green'></i>";
            } else {
                text = "<i class='ace-icon fa fa-circle red'></i>";
            }
            data = text
        }
        return data;
    },
}

The right thing should be:

$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
        ajax: "../php/staff.php",
        table: "#publicationTable",
        fields: [ {
                label: "Visualizado:",
                name: "p.id_Publication"
            }, {
                label: "Título da Publicação:",
                name: "ti.PublicationTitle"
            }, {
                label: "Tipo de Publicação:",
                name: "ty.PublicationType"
            }, {
                label: "Ano:",
                name: "p.ano"
            }, {
                label: "Mês:",
                name: "p.competencia"
            }, {
                label: "Empresa:",
                name: "c.razaoSocial"
            }, {
                label: "Favorecido:",
                name: "e.nome"
            }
        ]
    } );

    var table = $('#publicationTable').DataTable( {
        lengthChange: true,
        ajax: "../php/staff.php",
        columns: [
            {
                data: "p.status",
                render: function ( data, type, row ) {
                    var text = "";
                    if (type == "display") {
                        if (data == "1") {
                            text = "<i class='ace-icon fa fa-circle green'></i>";
                        } else {
                            text = "<i class='ace-icon fa fa-circle red'></i>";
                        }
                        data = text
                    }
                    return data;
                }
            },
            { data: "ti.PublicationTitle" },
            { data: "ty.PublicationType" },
            { data: "p.ano" },
            { data: "p.competencia" },
            { data: "c.razaoSocial" },
            { data: "e.nome" }
        ],
    } );
} );
  • I understood what you said, but it is still not returning <i class='ace-icon fa fa-Circle green'></i> or <i class='ace-icon fa fa-Circle red'></i>

  • @min.me Ae is already another problem right? Something appears on the console (F12)?

  • No @Guilhermenascimento, another error is returned;

  • @min.me and the Network tab (Network in Portuguese)?

  • Network only shows the files I use but no error

  • Exchange the lines for if (data == "1") {&#xA; text = "Foo";&#xA; } else {&#xA; text = "Bar"; and tell me if anything has changed. It’s just for a test this.

  • 1

    Now shows the foo and bar, but does not show the fa fa circle icon. pq??

  • Thank you already solved

  • @min.me then the problem is in CSS and nay in Javascript. Of details like this the CSS, because as I said, the problem is something else and the question has already changed from the original meaning.

  • All right then, already solved

  • @min.me so the javascript part my answer solved right? Please mark the answer as Correct if you wish, if you do not know how to do it read: https://pt.meta.stackoverflow.com/a/1079/3635

  • The other answer also says the same thing @Guilhermenascimento, can not accept 2?

  • @min.me Choice criterion is yours, you can choose which one was answered first or which answered most correctly. You can choose even by empathy (although not the ideal path).

Show 8 more comments

1


Not missing a key, is a syntax error occurring.

What is happening is that you have a comma that is in the wrong place, close to:

        return data;
    },   // <--Essa vírgula, tem que estar na linha debaixo                   
}

Being that correctly it should be so:

      return data;
    }                   
}, // <-- Agora sim, a vírgula no lugar correto

She should be on the bottom line. Finally, her code would look similar to this:

var editor; // use a global for the submit and return data rendering in the examples

    $(document).ready(function() {
        editor = new $.fn.dataTable.Editor( {
            ajax: "../php/staff.php",
            table: "#publicationTable",
            fields: [ {
                    label: "Visualizado:",
                    name: "p.id_Publication"
                }, {
                    label: "Título da Publicação:",
                    name: "ti.PublicationTitle"
                }, {
                    label: "Tipo de Publicação:",
                    name: "ty.PublicationType"
                }, {
                    label: "Ano:",
                    name: "p.ano"
                }, {
                    label: "Mês:",
                    name: "p.competencia"
                }, {
                    label: "Empresa:",
                    name: "c.razaoSocial"
                }, {
                    label: "Favorecido:",
                    name: "e.nome"
                }
            ]
        } );

        var table = $('#publicationTable').DataTable( {
            lengthChange: true,
            ajax: "../php/staff.php",
            columns: [
                { data: "p.status",
                render: function ( data, type, row ) {
                    var text = "";
                    if (type == "display") {
                        if (data == "1") {
                            text = "<i class='ace-icon fa fa-circle green'></i>";
                        } else {
                            text = "<i class='ace-icon fa fa-circle red'></i>";
                        }
                        data = text
                    }
                    return data;
                }
            },
                { data: "ti.PublicationTitle" },
                { data: "ty.PublicationType" },
                { data: "p.ano" },
                { data: "p.competencia" },
                { data: "c.razaoSocial" },
                { data: "e.nome" }
            ],
        } );
    } );
  • 2

    Downvoter, if you do not know how to vote you can read here https://answall.com/help/privileges/vote-up and here also https://answall.com/help/privileges/vote-down

Browser other questions tagged

You are not signed in. Login or sign up in order to post.