use resultset returned by WL.Server.invokeSQLState

2019-01-20 17:54发布

问题:

I want to use the resultSet returned by invoking a SQL statement through WL.Server.invokeSQLStatement statement in a SQL adapter procedure within the procedure itself.

I have tried the way we generally do by using result.invocationResult.resultSet[variable].Property inside the SQL adapter procedure, but it doesn't works.

HTML

    <div data-role="page" id="mainPage">
        <div data-role="header">search medicine</div>
            <br>

            <hr>
            <div data-role="content">       
                <input type="text" value="MEDICINE" id="medicine"><hr>
                <input type="text" value="LOCATION" id="location"><hr>
                <input type="submit" id="search" value="SEARCH">
            </div>
        </div>

        <div id="itemsList" data-role="page">
            <table id="myTable" border="1">
                <tr>
                    <th>Registration No</th>
                </tr>
            </table>    
            <div data-role="header">gg</div>
            <div data-role="content"></div>
        </div>

JavaScript

window.$ = window.jQuery = WLJQ;
function wlCommonInit() {

}

$(document).ready(function(){
    $("#search").click(function(){
        GetEmployeeData();  
    });
});

function GetEmployeeData() {        
    var medicine= $("#medicine").val();
    var location=$("#location").val();
    alert(medicine);
    var invocationData = {
        adapter : 'ATM',
        procedure : 'getStudentInfos',
        parameters: [medicine,location]
    };

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

function loadFeedsSuccess(result){      
    alert("Hii");
    WL.Logger.debug("Feed retrieve success");

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

function loadFeedsFailure(result){      
    alert("H");
}

function displayFeeds(items){
    alert("ii");
    var table = document.getElementById("myTable");
    for(var i=0;i<items.length;i++)
        {
            // Create an empty <tr> element and add it to the 1st position of the table:
            var row = table.insertRow(i+1); 
            var cell1 = row.insertCell(0);

            // Add some text to the new cells:
            cell1.innerHTML = items[i].RegNo;
        }
}

function LoadResultPage() {
    $("AppDiv").hide();
    $("#itemsList").show();
};

Adapter JavaScript

 var selectStatement2 = WL.Server.createSQLStatement("select LocId from location WHERE LocName= ? ");
    var selectStatement3 = WL.Server.createSQLStatement("select RegNo from storeloc WHERE LocId= ? ");

    function getStudentInfos(Name,Location) {    
        var result=WL.Server.invokeSQLStatement({
            preparedStatement : selectStatement2,
            parameters : [Location]
        });

       var a = result.invocationResult.resultSet;
       if(a.length>0)
       var LocId=a[0].LocId;
       return WL.Server.invokeSQLStatement({
        preparedStatement : selectStatement3,
            parameters : [LocId]
        });     
    }

On Chrome i am getting the following error

Procedure invocation error. Ecma Error: TypeError: Cannot read property "resultSet" from undefined (E%3A%5CPRACTICE%5CATM%5Cadapters%5CATM/ATM-impl.js#25).My line No 25 is var a= result.invocationResult.resultSet;

回答1:

Try changing your adapter JavaScript implementation to the following instead.

I tried a version of this with different table and variables and it worked for me (at least, the adapter invocation from Worklight Studio).

Note how I am using locId.resultSet and not locId.invocationResult.resultSet. The latter is used on the client-side whereas the former is used on the server-side.

Also note the separation to two functions.

function getStudentInfos(Name,Location) {
    var locId, a, b;

    locId = getlocId(Location);
    a = locId.resultSet;
    if (a && a.length>0) {
        b = a[0].LocId;
    }
    return WL.Server.invokeSQLStatement({
        preparedStatement : selectStatement3,
        parameters : [b]
    });
}

function getlocId (Location) {
    var result = WL.Server.invokeSQLStatement({
        preparedStatement : selectStatement2,
        parameters : [Location]
    });
    return result;
}

Note the if statement... make sure to add some error handling there as well...