Error DataTables warning: Requested unknown parameter calling a function inside render

Error DataTables warning: Requested unknown parameter calling a function inside render

AblitterAblitter Posts: 13Questions: 5Answers: 0
edited February 2022 in General

Hello everyone,

i have a problem calling a function inside the render function.
If i use the the code below all is working fine, but if i put the code inside a function and i call that function from within the render, i get the DataTables warning: Requested unknown parameter 'userId' for row 0, column 10. For more information about this error, please see http://datatables.net/tn/4

{ "data": "userInfo", "name": "userInfo", "render": function ( data, type, row ) {

    if (row.userId === 'undefined' || row.userId == null || row.userId == "") {
           return "-";
    } else if (row.name != null && row.name != "" && row.surname != null && row.surname!= "") {
        return row.userId + ' ' + row.name + ' ' + row.surname;
    } else {
        return row.userId;
    }
    
}

if i call a function inside the render, i get the error above

{ "data": "userInfo", "name": "userInfo", "render": function ( data, type, row ) {
    
    return formatUserInfo(row.userId, row.name, row.surname); 
}

I don't check for the row validity in the second case and probably some parameters are resulting empy or null.
Any suggestion to avoid this problem?

Answers

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    In some of your rows "row.userId" seems to be undefined. I tested with a non existing field called "bla".

    This code always gave me the data tables error:

    {   data: "ctr.number",
        render: function (data, type, row) {
            if (row.bla === 'undefined') {
                return data;
            }
        }
    },
    

    This code worked because I included "typeof". I have not seen your "undefined" syntax before to be honest. Seems to be causing issues.

    {   data: "ctr.number",
        render: function (data, type, row) {
            if (typeof row.bla === 'undefined') {
                return data;
            }
        }
    },
    

    This also worked:

    {   data: "ctr.number",
        render: function (data, type, row) {
            if (row.bla === undefined) {
                return data;
            }
        }
    },
    

    I would check your syntax and if that isn't the cause you should post a test case. Or, as a first step, the exact code of the function you are calling.

  • AblitterAblitter Posts: 13Questions: 5Answers: 0

    Thank you for the reply.

    The code in the first case works well.

    The code in the second case doesn't work.

    I call a function in the second case. This doesn't work, giving me the error described above.

    { "data": "userInfo", "name": "userInfo", "render": function ( data, type, row ) {
         
        return formatUserInfo(row.userId, row.name, row.surname);
    }
    

    The function formatUserInfo does exactly what you see in the first case.

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    edited February 2022

    Have you fixed the syntax error? Or is your field really a string with value "undefined"?

    To be clear: There is absolutely no difference between doing the rendering in a called function or not. I do this hundreds of times and it always works So there has to be a slight deviation in your code. But I see you don't want to post your exact code which is ok for me. But then I am unable to help you further.

    Please post a test case as per the forum rules.

  • AblitterAblitter Posts: 13Questions: 5Answers: 0

    @rf1234

    you're right about the 'undefined' syntax, btw.

    The correct syntax is without the '', that i alredy tested before.
    I used typeof also, but as said previously, the problem is not in the first case, but in the second case.

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    Again: Since you are not posting your code I can't help you. Thank you for your understanding.

  • AblitterAblitter Posts: 13Questions: 5Answers: 0

    this is the function code:

    function formatUserInfo(userId, userName, userSurname) {
        
       if (typeof userId === 'undefined' || userId == null || userId == "") {
               return "-";
        } else if (userName != null && userName != "" && userSurname != null && userSurname!= "") {
            return userId  + ' ' + userName + ' ' + userSurname;
        } else {
            return userId;
        }
        
    }
        
    
  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    We will need to see the problem to help debug. Please post a test case with an example of the data causing the issues and the render function you are trying to use.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    I tested your function. It worked fine with an undefined userID, a userID which is null and a userID which is an empty string. Couldn't find any issues.

  • AblitterAblitter Posts: 13Questions: 5Answers: 0

    Unfortunately i'm not authorized to post any other part of source or data related to this project. I will continue to use the first code as in the first case.
    Thank you anyway for the help.

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    edited February 2022

    A test case only requires the code you've already posted here and a sample (doesn't have to be the same) of the data that is causing the problem. Start with thie:
    http://live.datatables.net/

    Kevin

Sign In or Register to comment.