Error on loading/rendering data from array of objects

Error on loading/rendering data from array of objects

latinunitlatinunit Posts: 73Questions: 12Answers: 0
edited November 2022 in Free community support

Here is my array of objects containing data structure

[{"firstName":"David Llanos","_schema":"nms:recipient"},{"firstName":"David Test","_schema":"nms:recipient"},{"firstName":"David Test","_schema":"nms:recipient"},{"firstName":"David Test","_schema":"nms:recipient"},{"firstName":"D Test 2","_schema":"nms:recipient"}]

Here is my script

           ```
               var rcpObjData = document.controller.getValue('/ctx/vars/recipients')
               alert(rcpObjData);      
                $('#recipients').DataTable({
                data: rcpObjData,
                columns: [
                    { data: 'firstName' },
                   ],
                 });
            ```

here is my html div

<table class="display" id="recipients" style="width: 100%;"></table>

It wont render or recognise firstName

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    I'm not familiar with document.controller.getValue() but suspect it might be an asynchronous process. If it is then the Datatables initialization is happening before the data is returned. Is there some sort of success callback you can use to initialize and load the Datatables data?

    It doesn't look like you have a thead defined in HTML for the table. If not use columns.title to define the column headers.

    The problem could be one or both of these issues.

    Kevin

  • latinunitlatinunit Posts: 73Questions: 12Answers: 0
    edited November 2022

    Here is my script

    <script type="text/javascript">
    

    jQuery( document ).ready(function() {

    /********************************/
    
          var callbacks = {
            onComplete: function(){console.log('onComplete')},
            onError: function(a){console.warning('onError', a)},
            onSuccess: function(objects){
              console.log('onSuccess', objects)             
               document.controller.setValue('/ctx/vars/recipients', JSON.stringify(objects));
    
    
                /**********************************/
                   var rcpObjData = document.controller.getValue('/ctx/vars/recipients');
                   alert(rcpObjData);      
                    $('#recipients').DataTable({
                    data: rcpObjData,
                    columns: [
                        { data: 'firstName' },
                       ],
                     });
                /**********************************/
    
    
            },
    
          }
          var q = new NL.DataSource.QueryDef({
            schema: 'nms:recipient',
            select: {node: [
              {expr: '@firstName'},
            ]}, 
            where: {condition: [
              {expr: "@email = 'email@myemail'"},
            ]},
          });
          q.get(0,0,callbacks);
    

    });
    </script>

  • latinunitlatinunit Posts: 73Questions: 12Answers: 0

    The init is being done using a call back on success.

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    You probably don't want to use JSON.stringify(objects). Datatables expects a Javascript array but its turned into a string using JSON.stringify(objects). I would say that lines 8 and 12 aren't needed and you can just use objects in line 15, for example, data: objects,.

    Kevin

  • latinunitlatinunit Posts: 73Questions: 12Answers: 0

    I changed my script but still the error is the same

        /********************************/
        
    var postData = {
       "operation":"select", "schema":"nms:recipient",
       "startLine":0, "lineCount":99999,
       "select":{
          "node":[
            {"expr":"@firstName"},
          ]
       },
       "where":{
          "condition":[{"expr":"@email = 'myemailhere'"}]
       },
       "orderBy":{
          "node":[{"expr":"@lastModified", "sortDesc":true}]
       },   
    };
    $.post("/xtk/queryList.jssp", {queryDef:encodeURIComponent(JSON.stringify(postData))}, function(response) {
      console.log(response); 
      console.log(response.data); 
      
      /**********************************/
       document.controller.setValue('/ctx/vars/recipients', JSON.stringify(response.data));
       var rcpObjData = document.controller.getValue('/ctx/vars/recipients')
       alert(rcpObjData);      
        $('#recipients').DataTable({
            data: rcpObjData,
            columns: [
                { data: 'firstName' },
               ],
             });
    /**********************************/
      
    });
    
  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    Did you read my last post?

    You are still using JSON.stringify(response.data) which won't work!

    Kevin

  • latinunitlatinunit Posts: 73Questions: 12Answers: 0

    Ok, is fixed by parsing the string as json

    JSON.parse(rcpObjData),

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    You should be able to just use the response object without the need to JSON.stringify() followed by JSON.parse(). This just adds two steps that result in the same value.

    Kevin

  • latinunitlatinunit Posts: 73Questions: 12Answers: 0
    edited November 2022

    The thing is, on my application when I grab the value from the following application variable, is as a string, which is why i need to convert it to json

    var rcpObjData = document.controller.getValue('/ctx/vars/recipients')

    But you are correct, just using the response.data is enough and no conversion needed, thanks :)

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    when I grab the value from the following application variable, is as a string, which is why i need to convert it to json

    You are making it a string in this statement:

    document.controller.setValue('/ctx/vars/recipients', JSON.stringify(response.data));
    

    Kevin

Sign In or Register to comment.