Can't get foreach() to work

Posted by davez on 03-Aug-2015 16:21

I'm trying to retrieve records from an OE Mobile REST service and parse them in a Telerik Mobile app. I'm using the Progress JSDO 4.0. When I make a call to my login service, this is the response:

{"response":{"ttLogin":{"ttLogin":[{"EmpID":"","FullName":"","Groups":"","Lang":"","Prefs":"","Priv":"","LoginStatus":"Invalid username/password."}]}}}

I have an afterInvoke function that runs, but using a foreach() in it doesn't do anything.

function onAfterInvoke(jsdo, success, request) {
    if (success) {
        alert('onAfterInvoke success');
        jsdo.foreach(function (login) {
            alert("In foreach");
        });
    } else {
        alert('onAfterInvoke error');
    }
};

I get the "onAfterInvoke success" message, but no "in foreach" messages, even though there's one record in the response. I think the two ttLogin elements in the response might be the problem, but I don't know how to get rid of the second one to try that. Any ideas? Thanks.

All Replies

Posted by egarcia on 03-Aug-2015 17:13

Hello,

Based on your description, it looks like the foreach() is running but the JSDO memory is empty.

(You can confirm this with the debugger.)

By default, invoke operations are not automatically added to the JSDO memory (catalogs from Rollbase have an option to merge the data automatically).

This because, in general, invoke operations may not necessarily have the same schema as your resource,

Since in this case, it looks like the schema in the invoke operation is the same in the schema, you could to use one of the following approaches to access the data:

- Process the data from the request parameter.

Example:

console.log(request.response.ttLogin.ttLogin[0].LoginStatus);

- Use jsdo.addRecords(request.response.ttLogin, progress.data.JSDO.MODE_EMPTY) to add the records in the response to the JSDO memory. (You can pass the temp-table (ttLogin parameter - request.response.ttLogin) or the array (request.response.ttLogin.ttLogin).)

I I hope this helps.

Posted by davez on 04-Aug-2015 11:13

Thanks Edsel! The "request.response... " worked. I didn't realize that invoke operations don't populate the JSDO memory. You said it might have a different schema than the resource. But shouldn't it use what is listed in the service's JSON file?

Posted by egarcia on 04-Aug-2015 12:06

You are welcome.

> But shouldn't it use what is listed in the service's JSON file?

Not necessarily. The schema listed in the catalog file corresponds to the schema for the resource (DataSet and Temp-Tables - they do not have to match exactly the database).

Invoke operations are not required to have the same schema as the resource.

For example, you could have a Business Entity for customers and have an invoke operation (as a support method) to return data for orders.

The CRUD operations use the schema and you can use fill(), add(), assign(), remove(), and saveChanges() to access customers.

A method called "getOrders" could be used to return orders to a given customer.

Regards.

Posted by Shelley Chase on 04-Aug-2015 12:30

Hi Dave,

I agree with you that if the same PDS or TT is used for an invoke operation that we should provide the option to update the JSDO memory on return from the call. This is on our futures list but there is a simple workaround of using addRecords. Although you have to know ahbout it to use it :-)

Maybe a sample would be useful.

Thanks

-Shelley

Posted by davez on 04-Aug-2015 14:49

Thanks Shelly. That option would be great. Or at least something in the documentation describing the workaround.

This thread is closed