0

This is my stored procedure:

CREATE PROCEDURE PROC()
BEGIN
SELECT * FROM TABLENAME;
END//

This is my unction to call stored procedure using SQL adapter:

function callStored() {
    return WL.Server.invokeSQLStoredProcedure({
        procedure : "proc",
        parameters : []
    });
}

This is the invocationResult:

{
   "isSuccessful": true,
   "resultSet": [
      {
         "name": "a",
         "pass": "123",
         "time_stamp": "2014-04-07T10:13:17.000Z"
      },
      {
         "name": "chetan",
         "pass": "123456",
         "time_stamp": "2014-04-07T10:13:34.000Z"
      },
      {
         "name": "dileep",
         "pass": "456321",
         "time_stamp": "2014-04-07T10:13:54.000Z"
      },
      {
         "name": "bnc",
         "pass": "654321",
         "time_stamp": "2014-04-07T10:19:37.000Z"
      }
   ]
}

I need to parse this and display or alert the values of name, pass and time_stamp.

How do I accomplish this?

Idan Adar
  • 44,156
  • 13
  • 50
  • 89

2 Answers2

0

In your application JavaScript (common\js\main.js), you can have something like the following. In the below code an alert will be displayed with the values of the name key from the resultSet.

You can also take a look here: use resultset returned by WL.Server.invokeSQLStatement within sql adapter procedure

function wlCommonInit() {
    invokeAdapter();
}

function invokeAdapter() {
    var invocationData = {
        adapter: "your-adapter-name",
        procedure: "callStored",
        parameters: []
    };

    WL.Client.invokeProcedure (invocationData, {
        onSuccess: invocationSuccess,
        onFailure: invocationFailure}
    );
}

function invocationSuccess(response) {
    var i,
        resultSet = response.invocationResult.resultSet;
        namesArray = [];

    for (i = 0; i < resultSet.length; i++) {
        namesArray[i] = resultSet[i].name;
    }
    alert (JSON.stringify(namesArray));
}

function invocationFailure(response) {
   alert (response);
}

You have already asked this here: ibm worklight stored procedure
Why did you not follow through the documentation and learned how to write the above?

Please read the documentation!

Community
  • 1
  • 1
Idan Adar
  • 44,156
  • 13
  • 50
  • 89
  • 3
    A simple way to interact with the 'invocationResult' using JQuery, which is embedded by default in WL hybrid apps: var nn = new Array(), pp = new Array(), tt = new Array(); $(response.invocationResult.resultSet).each(function(idx, data){ nn[idx] = data.name; pp[idx] = data.pass; tt[idx] = data.time_stamp; }); console.log(nn); console.log(pp); console.log(tt); – eabe Apr 09 '14 at 08:29
0

Please read the "Invoking adapter procedures from client applications" and its Exercise and code sample in the Getting started with IBM Worklight page.

function wlCommonInit(){
    getUsersInfo();
}

function getUsersInfo(){
    var invocationData = {
        adapter : 'YOUR_ADAPTER',
        procedure : 'YOUR_PROCEDURE',
        parameters : []
    };

    WL.Client.invokeProcedure(invocationData,{
        onSuccess : getUsersInfoSuccess,
        onFailure : getUsersInfoFailure
    });
}

function getUsersInfoSuccess(result){
    if (result.invocationResult.Items.length > 0) {
        displayUsersInfo(result.invocationResult.Items);
    } else { 
        getUsersInfoFailure();
    }
}

function getUsersInfoFailure(result){
    alert("Cannot retrieve users info");
}

function displayUsersInfo(items){
    var i = 0, usersInfo = '';
    for (i = 0; i < items.length; i++) {
        usersInfo += ' name: ' + items[i].name;
        usersInfo += ' pass: ' + items[i].pass;
        usersInfo += ' time_stamp: ' + items[i].time_stamp;
    }
    alert(usersInfo);
}
Raanan Avidor
  • 3,533
  • 4
  • 25
  • 32
  • can i write this function in httpadapter.js file and write wl.server.invokeProcedure() insted of wl.client – user3510619 Apr 09 '14 at 10:38
  • @user3510619, no you cannot - please read the training module as it explains exactly what you're asking. You are confusing between client-side and server-side. – Idan Adar Apr 09 '14 at 10:53