Uncaught SyntaxError: Unexpected token {[closed]

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 wonder why this error is occurring, and how can I resolve it?

    
asked by anonymous 14.08.2017 / 22:49

2 answers

1

Not missing a key , is a syntax error that is occurring.

What is happening is that you have a comma that is in the wrong place near:

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

Being rightly supposed to look like this:

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

She should be on the bottom line. Finally, your code would look something like 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" }
            ],
        } );
    } );
    
14.08.2017 / 22:53
4

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 an array , for every {data:...} must have a comma to separate, then it lacks before { data: "ti.PublicationTitle" } comma to separate :

    { 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 correct one 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" }
        ],
    } );
} );
    
14.08.2017 / 22:55