editor upload file

editor upload file

calacala Posts: 52Questions: 15Answers: 0
edited September 2015 in Free community support

Hi everybody...

I've created a dataTable with upload feature.
When i create a new line with the editor (without uploading the image), it is saved and showed in my page. When I also add the image, an error appear in the console: "Uncaught TypeError: Cannot read property 'id' of undefined".

Now, if I look at the SQL file table, the "file row" is created but the corresponding field in the other SQL table has not the id of the image.

If I force the id in the field with an external sql software, the image is shown in the table.

I've followed step-by-step the example, but I cannot figure out how to correct the error.

<?php
 
$path=realpath(dirname(__FILE__).'/../'.'Editor/php/DataTables.php');

require(realpath($path));

// Alias Editor classes so they are easy to use
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate;



    $data=Editor::inst( $db, 'wp__register', 'id')
    ->fields(

...some text fields...

        Field::inst( 'wp_image' )
            ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/wp-content/uploads/__ID__.__EXTN__' ) 
                ->db( 'wp_image', 'id', array(
                    'filename'    => Upload::DB_FILE_NAME,
                    'filesize'    => Upload::DB_FILE_SIZE,
                    'web_path'    => Upload::DB_WEB_PATH,
                    'system_path' => Upload::DB_SYSTEM_PATH
                ) )
       
            )
            ->setFormatter( 'Format::nullEmpty' )
    )
    ->process($_POST)
    ->data();
    echo json_encode( $data );
?>

and in the js:

var editor;
 
$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
   
        "ajax": {
            "url": "../server_processing_editor_9_9.php",
            "type": "POST"
        },
        "table": "#protocol_table",
        fields: [ 

...

            {
                label: "Allegati:",
                name: "wp_image",
                type: "upload",
                display: function ( file_id ) {
                    
                    return '<img src="'+table.file( 'wp_image', file_id ).web_path+'"/>';
                },
                clearText: "Clear",
                noImageText: 'No image'
            }
        ]
    } );
    
    var table=$('#protocol_table').DataTable( {
        
        
        
        "ajax": {
            "url": "../server_processing_editor_9_9.php",
            "type": "POST"
        },
        columns: [
            { data: "wp_image",
                render: function ( file_id ) {
                    console.log(table.file( 'wp_image', file_id));
                    console.log(file_id);
                    return file_id ?
                        '<img src="'+table.file( 'wp_image', file_id).web_path+'"/>' :
                        null;
                },
                defaultContent: "No image",
                title: "Image"
            }
        ],

        "select": true,
        "processing": true,
        dom: 'Bfrtip',
        buttons: [
            { text: 'Reload',
                action: function ( e, dt, node, config ) {
                //alert("I am an alert box!");
                dt.ajax.reload();
            }},
            { extend: "create", editor: editor },
            { extend: "edit",   editor: editor }
            
        ],
        "paging":   true,
        "order": [[ 1, "asc" ]],
        "info":     true,
        "filter":   true,
      
        "language": {
        "lengthMenu": "Visualizza _MENU_ records per pagina",
            "zeroRecords": "Nessun record trovato",
            "info": "Pagina _PAGE_ di _PAGES_",
            "infoEmpty": "Nessun record disponibile",
            "infoFiltered": "(Filtrati da _MAX_ record totali)"
         }
    } );
} );

debug link: http://debug.datatables.net/afowuf

Thank you for the help!

Answers

  • allanallan Posts: 61,946Questions: 1Answers: 10,158 Site admin

    Are you able to give a link to the page so I can check it out please? I don't immediately see what the issue would be there I'm afraid.

    Thanks,
    Allan

  • allanallan Posts: 61,946Questions: 1Answers: 10,158 Site admin

    Also - does it still give the error after a refresh? Or does it load the table correctly at that point?

  • calacala Posts: 52Questions: 15Answers: 0

    thank you for the reply!

    no... I cannot give it because is a private development site.

    the problem is not in the load of the table, is the connection between the wp_image field in the register table (wp__register) and the id of the file table.

    The id of the file is not copied into the corresponding field in the other table.

    For debug, in which function does the code update the "data" table? I know that the file table is updated after the file to be loaded is chosen. Could you give me an example of the 2 sql table involved (that two of the example on https://editor.datatables.net/examples/advanced/upload.html for example)?

    Thank you for the help!

  • allanallan Posts: 61,946Questions: 1Answers: 10,158 Site admin

    Could you give me an example of the 2 sql table involved

    The SQL schema from that example is available here. The tables are users_files and files.

    The id of the file is not copied into the corresponding field in the other table.

    At all? Or just when the file is uploaded? That was the point of my question about the reload - does the image show when you reload the page, but not initially?

    Thanks,
    Allan

  • calacala Posts: 52Questions: 15Answers: 0
    edited September 2015

    thank you for another reply (and for the tables, I'll study them carefully)!

    no... the id is not copied at all! when I press create/update in the form, the console error "Uncaught TypeError: Cannot read property 'id' of undefined" has occurred; I think this is the reason of the non-copy, but I cannot figure out who is that "undefined" object/field/thing and how to set it.

    In addition to the error, the window of the Editor does not close.

    Thanks!

  • allanallan Posts: 61,946Questions: 1Answers: 10,158 Site admin

    What is the JSON that the server returns when a file is uploaded?

    Allan

  • calacala Posts: 52Questions: 15Answers: 0

    I've created the structure for the multi-upload and I can upload the files on the database table. This is the server response:

    {
        "data":[
            {
            "DT_RowId":"row_1",
            "wp_register":
                {"protocollo":"1",
                "user":"2werfg",
                "data_imm":"2015-09-17",
                "anno_eccl":"",
                "sotto_cod":"aRM",
                "data_doc":"",
                "rif":"",
                "num_ord":"",
                "tipo_doc":"Lettera",
                "mittente":"",
                "destinatario":"",
                "oggetto":"",
                "per_mod":"Si"},
                "wp_files":[]},
            {
            "DT_RowId":"row_2",
            "wp_register":{
                "protocollo":"2",
                "user":"sdfgbh",
                "data_imm":"2015-09-12",
                "anno_eccl":"1234",
                "sotto_cod":"aRM",
                "data_doc":"",
                "rif":"",
                "num_ord":"",
                "tipo_doc":"Lettera",
                "mittente":"",
                "destinatario":"",
                "oggetto":"",
                "per_mod":"Si"},
                "wp_files":[]}
        ],
    "options":[],
    "files":{
        "wp_files":{
            "1":{
                "id":"1",
                "filename":"sort_desc.png",
                "filesize":"1127",
                "web_path":"\/wp-content\/uploads\/1.png",
                "system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/1.png"
            },
            "2":{
                "id":"2",
                "filename":"details_close.png",
                "filesize":"841",
                "web_path":"\/wp-content\/uploads\/2.png",
                "system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/2.png"}
            }
    }}
    

    When I press Create/Update on the editor form the error is always the same.

    Why in the example page is not written to create the "mid" table that connect user id and files id? Is it not necessary? Does the Editor self-create it?

    I need to understand where and which instructions create the link between the tables in the database. Could you tell me it, please?

  • allanallan Posts: 61,946Questions: 1Answers: 10,158 Site admin

    Could you show me the JSON that is returned when the file has been uploaded? That should also contain information about the files that have been uploaded (otherwise the file information will be out of date and you'll likely get the error you are seeing).

    Allan

  • calacala Posts: 52Questions: 15Answers: 0

    In my previous post from line 39 there are the information about the uploaded files. That is the output of the

    "ajax": {
                "url": "../server_processing_editor_9_9.php",
                "type": "POST"
            },
    

    file.
    What other information do you need and what do I have to add to the scripts to get them?
    Thank you so much!

  • allanallan Posts: 61,946Questions: 1Answers: 10,158 Site admin

    Hi,

    The information shown above looks like the JSON from when the DataTable is initialised - not the JSON from when the file is uploaded. Is that the case? If you look in the "Network" tab of your browser's web developer tools you should be able to see an Ajax request being made immediately when a file is selected. I'm interested to know what the returned data is from that request.

    Allan

  • calacala Posts: 52Questions: 15Answers: 0
    {"data":[],
    "files":{"wp_files":{"1":{"wp_files.id":"1","filename":"sort_desc.png","filesize":"1127","web_path":"\/wp-content\/uploads\/1.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/1.png"},
    "2":{"wp_files.id":"2","filename":"details_close.png","filesize":"841","web_path":"\/wp-content\/uploads\/2.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/2.png"},
    "22":{"wp_files.id":"22","filename":"sort_asc.png","filesize":"1118","web_path":"\/wp-content\/uploads\/22.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/22.png"},
    "23":{"wp_files.id":"23","filename":"shadow_right.png","filesize":"1202","web_path":"\/wp-content\/uploads\/23.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/23.png"},
    "24":{"wp_files.id":"24","filename":"details_close.png","filesize":"841","web_path":"\/wp-content\/uploads\/24.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/24.png"},
    "25":{"wp_files.id":"25","filename":"details_open.png","filesize":"881","web_path":"\/wp-content\/uploads\/25.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/25.png"},
    "26":{"wp_files.id":"26","filename":"close.png","filesize":"1777","web_path":"\/wp-content\/uploads\/26.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/26.png"},
    "27":{"wp_files.id":"27","filename":"details_open.png","filesize":"881","web_path":"\/wp-content\/uploads\/27.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/27.png"},
    "28":{"wp_files.id":"28","filename":"details_close.png","filesize":"841","web_path":"\/wp-content\/uploads\/28.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/28.png"},
    "29":{"wp_files.id":"29","filename":"sort_asc.png","filesize":"1118","web_path":"\/wp-content\/uploads\/29.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/29.png"},
    "30":{"wp_files.id":"30","filename":"sort_desc.png","filesize":"1127","web_path":"\/wp-content\/uploads\/30.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/30.png"},
    "31":{"wp_files.id":"31","filename":"sort_desc.png","filesize":"1127","web_path":"\/wp-content\/uploads\/31.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/31.png"},
    "32":{"wp_files.id":"32","filename":"calender.png","filesize":"235","web_path":"\/wp-content\/uploads\/32.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/32.png"}}},
    "upload":{"id":"32"}}
    

    This one? I've uploaded in the first data in the table, the id=32 file.

  • allanallan Posts: 61,946Questions: 1Answers: 10,158 Site admin

    Yes - thank you.

    Uncaught TypeError: Cannot read property 'id' of undefined

    Does it tell you which line this occurs on, and what the error backtrace is please?

    The data shown above looks good.

    Thanks,
    Allan

  • calacala Posts: 52Questions: 15Answers: 0

    dataTables.editor.js:319
    Uncaught TypeError: Cannot read property 'id' of undefined

    The data shown above looks good.

    Finally a good news :)

  • allanallan Posts: 61,946Questions: 1Answers: 10,158 Site admin

    Are you able to give me a full backtrace of the error please? Unfortunately that is the encoded version of Editor so that line number isn't going to mean very much.

    Also to confirm, this error shows up immediately after you load the file?

    Are you able to PM me a link to the page, it would make debugging it much quicker!

    Allan

  • calacala Posts: 52Questions: 15Answers: 0

    I've copied verbatim the file upload example in order to test if something was wrong in my file. I've updated the files only with the correct paths of the datatable libraries and the save path for the uploaded files.

    Same error in the same line... Could you provide me a previous version of the Editor?

  • calacala Posts: 52Questions: 15Answers: 0

    Are you able to give me a full backtrace of the error please? Unfortunately that is the encoded version of Editor so that line number isn't going to mean very much.

    that line is in this piece of code of dataTables.editor.js:

    ,success:function(b){var c0d="UR";var c6="readAsData";var u8d="push";var K77="Erro";var D1d="Err";var X57="reS";a[(R6d+P8)]((g4+X57+S8d+M+S77+s0+M1+w8+J6l+B5d+N3+i3));if(b[(X5d+b4d+M0+U5l+R6d+Q5l)]&&b[(Z8+h3+S6d+i3+D1d+E3+J4)].length)for(var b=b[(X5d+K2d+K2+K77+Q5l)],e=0,h=b.length;e<h;e++)a.error(b[e][(a0l+o6d+h3)],b[e][(i7d)]);else b.error?a.error(b.error):(b[A2]&&d[(r6d+j37)](b[(n2d+J4)],function(a,b){var A3l="ile";f[(X5d+A3l+J4)][a]=b;}
    ),g[u8d](b[(G2+g4+S6d+f6+i3)][(g2d)]),n<c.length-1?(n++,i[(c6+c0d+k0)](c[n])):(m[f6d](a,g),l&&a[a3l]()));}
    }
    

    Also to confirm, this error shows up immediately after you load the file?

    Not immediately after the upload (which is successful), but when the create/update button of the Editor form is pressed.

    Are you able to PM me a link to the page, it would make debugging it much quicker!

    No, it is on a private server.

  • allanallan Posts: 61,946Questions: 1Answers: 10,158 Site admin

    Not immediately after the upload (which is successful), but when the create/update button of the Editor form is pressed.

    Ah. Okay, in which case, can you give me the JSON response that the server is returning from its Ajax request as a result of the submit button please? Or does the error occur before the Ajax request is even sent?

    Sorry for asking so many questions - its difficult to understand the issue without being able to see it :-)

    Allan

  • calacala Posts: 52Questions: 15Answers: 0
    edited September 2015

    This is the reply at the upload of the 42 file on row_1:

    {"data":[],
    "files":{"wp_files":{
    "1":{"id":"1","filename":"sort_desc.png","filesize":"1127","web_path":"\/wp-content\/uploads\/1.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/1.png"},
    "2":{"id":"2","filename":"details_close.png","filesize":"841","web_path":"\/wp-content\/uploads\/2.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/2.png"},
    "41":{"id":"41","filename":"details_close.png","filesize":"841","web_path":"\/wp-content\/uploads\/41.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/41.png"},
    "42":{"id":"42","filename":"calender.png","filesize":"235","web_path":"\/wp-content\/uploads\/42.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/42.png"}}},
    "upload":{"id":"42"}}
    

    and this when the submit button is pressed:

    {"data":[{
    "DT_RowId":"row_1","wp_register":
    {"protocollo":"1","user":"2werfg","data_imm":"2015-09-17","anno_eccl":"","sotto_cod":"aRM","data_doc":"","rif":"","num_ord":"","tipo_doc":"Lettera","mittente":"","destinatario":"","oggetto":"","per_mod":"Si","files":"1"}},
    {"DT_RowId":"row_2","wp_register":{"protocollo":"2","user":"sdfgbh","data_imm":"2015-09-12","anno_eccl":"1234","sotto_cod":"aRM","data_doc":"","rif":"","num_ord":"","tipo_doc":"Lettera","mittente":"","destinatario":"","oggetto":"6666q346tgdx","per_mod":"Si","files":"2"}},
    {"DT_RowId":"row_3","wp_register":{"protocollo":"34","user":"awsedf","data_imm":"2015-09-05","anno_eccl":"345","sotto_cod":"aRM","data_doc":"oggi","rif":"","num_ord":"","tipo_doc":"Lettera","mittente":"","destinatario":"","oggetto":"","per_mod":"Si","files":null}}],
    "options":[],
    "files":{"wp_files":{
    "1":{"id":"1","filename":"sort_desc.png","filesize":"1127","web_path":"\/wp-content\/uploads\/1.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/1.png"},
    "2":{"id":"2","filename":"details_close.png","filesize":"841","web_path":"\/wp-content\/uploads\/2.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/2.png"},
    "41":{"id":"41","filename":"details_close.png","filesize":"841","web_path":"\/wp-content\/uploads\/41.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/41.png"},
    "42":{"id":"42","filename":"calender.png","filesize":"235","web_path":"\/wp-content\/uploads\/42.png","system_path":"\/Library\/WebServer\/wordpress\/wp-content\/uploads\/42.png"}}}}
    

    You can see that the row_1 isn't updated (it has the old files id=1).

    Could it be a problem that I'm using DataTable in Wordpress?

    Thank you for the help!

  • allanallan Posts: 61,946Questions: 1Answers: 10,158 Site admin

    That's very odd. Yet another question to try and see what is going on I'm afraid - what is the data that is submitted to the server when you hit the submit button The network tab should have an option to show that as well. I'm wondering if it sent 42 or 1?

    I'm not seeing why that would cause an id error though.

    Allan

  • calacala Posts: 52Questions: 15Answers: 0

    Gotcha...

    Instead of

    "ajax": {
                "url": "../server_processing_editor_9_9.php",
                "type": "POST"
            },
    

    you need to use:

    ajax: "../server_processing_editor_9_9.php",
    

    in the javascript file.
    Why the firstscript call 2 times the upload function?

    Thanks allan for the help!

  • allanallan Posts: 61,946Questions: 1Answers: 10,158 Site admin

    Good to hear that mostly worked.

    Why the firstscript call 2 times the upload function?

    I don't understand? Could you link to a page showing the issue.

    Allan

This discussion has been closed.