Invoking Worklight Adapter and Displaying that JSON data in list view as strings

本小妞迷上赌 提交于 2019-12-23 05:00:21

问题


This is my adapter.impl.js code.

 //This procedure implementation is to get all the details from the table which in database.
 var procedure1Statement = WL.Server.createSQLStatement("select * from employee");
 function procedure1() {
return WL.Server.invokeSQLStatement({
    preparedStatement : procedure1Statement,
    parameters : []
});
}

This is my JSON data retrieved from SQL Adapter in worklight.

{
"isSuccessful": true,
"resultSet": [
  {
     "EMAIL": "bkandregula@gmail.com",
     "ID": 1,
     "NAME": "Bhanu Kandregula"
  },
  {
     "EMAIL": "rbkandregula@gmail.com",
     "ID": 2,
     "NAME": "Raghu Kandregula"
  },
  {
     "EMAIL": "shyamsurisetty@gmail.com",
     "ID": 3,
     "NAME": "Shyam Surisetty"
  },
  {
     "EMAIL": "bunny@gmail.com",
     "ID": 4,
     "NAME": "Bunny"
  },
  {
     "EMAIL": "divya@gmail.com",
     "ID": 5,
     "NAME": "Divya Sri"
  },
  {
     "EMAIL": "chandhu@gmail.com",
     "ID": 6,
     "NAME": "Chandana"
  }
]
}

Not I was to invoke this adapter from Client side and display this data in list view on my mobile console. For that, this is the code which I used in client side js file.

function wlCommonInit(){
LoadSQLRecords();}

function loadSQLRecords(){
var invocationData = {
    adapter : 'MySQLadap',
    procedure : 'procedure1',
    parameters : []
};

WL.Client.invokeProcedure(invocationData,{
    onSuccess : loadSQLQuerySuccess,
    onFailure : loadSQLQueryFailure
});
}

function loadSQLQuerySuccess(result) {
    WL.Logger.debug("Retrieve success" +  JSON.stringify(result));
    displayFeeds(result.invocationResult.resultSet);
}

function loadSQLQueryFailure(result) {
    WL.Logger.error("Retrieve failure");
}

function displayFeeds(items) {
    var ul = $('#itemsList');
    for (var i = 0; i < items.length; i++) {
        var li = $('<li/>').html(items[i].id);
        li.append($('<li/>').html(items[i].name));
        li.append($('<li/>').html(items[i].email));
        li.append($('<hr>'));
        ul.append(li);
    }
  }

And this my HTML file code to display my data on screen.

<div id="wrapper">
        <ul id="itemsList"></ul>
 </div>

But This is not working with me. I have been searching many blogs and sites, But I was unable to trigger this. Please do help me in invoking adapter and display that data in list view on mobile console. Thank you.


回答1:


You need to correct the below and then re-deploy the application.

  1. You have misspelled a function name:

    LoadSQLRecords should be loadSQLRecords.

    function wlCommonInit(){
        LoadSQLRecords(); // Upper-case 'L'; should be lower-case.
    }
    
    function loadSQLRecords(){
        var invocationData = {
            adapter : 'MySQLadap',
            procedure : 'procedure1',
            parameters : []
        };
    };
    

    You should have seen the following in the Chrome DevTools' console prior to the fix:

    worklight.js:5147 Uncaught Exception: Uncaught ReferenceError: LoadSQLRecords is not defined at (compiled_code):4
    main.js:4 Uncaught ReferenceError: LoadSQLRecords is not defined
    
  2. You need to change the following in order to actually display any values.

    • use items.invocationResult.resultSet
    • use correct capitalization for the properties

    From:

    for (var i = 0; i < items.length; i++) {
        var li = $('<li/>').html(items[i].id);
        li.append($('<li/>').html(items[i].name));
        li.append($('<li/>').html(items[i].email));
        li.append($('<hr>'));
        ul.append(li);
    }
    

    To:

    for (var i = 0; i < items.invocationResult.resultSet.length; i++) {
        var li = $('<li/>').html(items.invocationResult.resultSet[i].ID);
        li.append($('<li/>').html(items.invocationResult.resultSet[i].NAME));
        li.append($('<li/>').html(items.invocationResult.resultSet[i].EMAIL));
        li.append($('<hr>'));
        ul.append(li);
    }
    


来源:https://stackoverflow.com/questions/27858282/invoking-worklight-adapter-and-displaying-that-json-data-in-list-view-as-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!