Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

javascript - IBM Worklight - Unable to display data retrieved using SQL adapter

I am trying to fetch data from a database and display it in the app.
Below is my code. The invokation is doing well, but the data is not displayed.

sqlAdapter-impl.js

var selectStatement = WL.Server.createSQLStatement("select * from studentinfo");

function getStudentInfos() {    
    return WL.Server.invokeSQLStatement({
        preparedStatement : selectStatement,
        parameters : []
    });
}

sqlAdapter.js

window.$ = window.jQuery = WLJQ;

function wlCommonInit() {
    GetEmployeeData();
}

function GetEmployeeData() {
    var invocationData = {
        adapter : 'sqlAdapter',
        procedure : 'getStudentInfos'
    };

    WL.Client.invokeProcedure(invocationData,{
        onSuccess : loadFeedsSuccess,
        onFailure : loadFeedsFailure
    });
}

function loadFeedsSuccess(result){
    WL.Logger.debug("Feed retrieve success");
    busyIndicator.hide();
    if (result.invocationResult.Items.length>0) 
        displayFeeds(result.invocationResult.Items);
    else 
        loadFeedsFailure();
}

function displayFeeds(items){
    var ul = $('#itemsList');
    for (var i = 0; i < items.length; i++) {
        var li = $('<li/>').html(items[i].sid);
        var pubDate = $('<div/>', {'class': 'pubDate'}).html(items[i].sname);
        li.append(pubDate);     
        ul.append(li);
    }
}

sqlAdapter.html

<body id="content" style="display: none;">
    <div id="itemsList"></div>

    <script src="js/initOptions.js"></script>
    <script src="js/sqlAdapter.js"></script>
    <script src="js/messages.js"></script>
</body>

this is what I get on invoking the procedure

    {
    "isSuccessful": true,
    "resultSet": [
    {
     "sclass": "PUC",
     "sgrade": "A+",
     "sid": "PUC001",
     "sname": "Rohan"
     },
     {
     "sclass": "PUC",
     "sgrade": "A",
     "sid": "PUC002",
     "sname": "Rakesh"
     },
    {
     "sclass": "PUC",
     "sgrade": "C",
     "sid": "PUC003",
     "sname": "Raj"
     },
     {
     "sclass": "PUC",
     "sgrade": "E",
     "sid": "PUC004",
     "sname": "Roman"
      }
      ] 
      }

I just want all these things to be printed on the screen

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First, you need to change "Items":

if (result.invocationResult.Items.length>0) 
        displayFeeds(result.invocationResult.Items);

To "resultSet":

if (result.invocationResult.resultSet.length>0) 
        displayFeeds(result.invocationResult.resultSet);


Second, I've used the worklight_training database script Worklight provides in conjunction with your code snippets from the question. I've altered the displayFeeds function to this:

function displayFeeds(items) {
    var ul = $('#itemsList'), i, li;

    for (i = 0; i < items.length; i += 1) {     
        // Create new <li> element and populate it
        li = $('<li/>').text(item.firstName);   
        // Append the <li> element to the <ul> element
        ul.append(li);      
    }
}

The end result was displaying the firstName values from the users table.
You can then play with your JavaScript a bit more to display what you want from your database table...

enter image description here

BTW, as you can see... the console in Chrome Dev Tools does show something... which you claim in your case it does not. You need to figure that out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...