Hello All,

I want to create list/array of objects in JavaScript and use this list as a output parameter of the same JavaScript in mobile app. How to achieve this?

Thanks in advance

Prajakta 



Solution

Hi Prajakta,

OutSystems does not allow you to define an output parameter of data type list.

Only basic data types are allowed.

So you would need to serialize the javascript list to a JSON string using javascript JSON.stringify()

Then in OutSystems you can use the JSON Deserialize action to convert the JSON string to a structure of type list.

Regards,

Daniel

Solution

Hi again,

Attached an example on how to do it.

Regards,

Daniel

Hello Daniel,

Thank you so much for quick reply, I want to create list run time. I am trying to use push function but it is giving me an error

For example : 

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Lemon");

any pointer on this will solve my problem

Thanks & Regards

Prajakta

Hi,

Your javascript code is valid javascript code, that can not generate an error.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Lemon");

It should really matter how you create the list, static or dynamically.

Attached an updated example with exactly your javascript code working.

The key is to deserialize to a Text List in your case as the javascript object is just an array of strings.

Regards,

Daniel


Hi Prajakta,

I believe what you want to do, is you probably have some table list, and you would like to iterate over them and create json array out of it, and then used somewhere.

So, your function has to have a trigger point, maybe a button/document ready, and then it should go trough out the table list, create the json array, then you convert it with JSON.stringify() to a string, and then you can store that with JS, into a hidden input. (with class: Display:none;!) then on your action you could simple retrieve/send  the json string in outsystems.

with best regards,
Slavi Popov


Daniël Kuhlmann wrote:

Hi,

Your javascript code is valid javascript code, that can not generate an error.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Lemon");

It should really matter how you create the list, static or dynamically.

Attached an updated example with exactly your javascript code working.

The key is to deserialize to a Text List in your case as the javascript object is just an array of strings.

Regards,

Daniel


I am getting "TypeError: Cannot read property 'push' of undefined" on console. I will share sample oml tomorrow.

Thanks for the help!!


Hi,

Do you get that error with the last sample app i posted? I tested it and worked for me.

Regards,

Daniel

Slavi Popov wrote:

Hi Prajakta,

I believe what you want to do, is you probably have some table list, and you would like to iterate over them and create json array out of it, and then used somewhere.

So, your function has to have a trigger point, maybe a button/document ready, and then it should go trough out the table list, create the json array, then you convert it with JSON.stringify() to a string, and then you can store that with JS, into a hidden input. (with class: Display:none;!) then on your action you could simple retrieve/send  the json string in outsystems.

with best regards,
Slavi Popov


Actually I am working on Leaflet Map, there I am plotting some markers on map and with the help of Draw , If I draw circle around suppose two markers then those two markers should display(which is achieved already with the help of JavaScript). now I want to pass the list of these selected markers to other screen. 


Daniël Kuhlmann wrote:

Hi,

Do you get that error with the last sample app i posted? I tested it and worked for me.

Regards,

Daniel

Below is the sample code, please do let me know if I am doing something wrong

I defined var selectedMarkers at the very beginning 

Thanks

Prajakta

If you get the error  "TypeError: Cannot read property 'push' of undefined" then I expect that the object onto which you want to push something is not an array. .push only works for arrays in javascript (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push)

Maybe you forgot to initialize the selectedMarkers variable like:

var selectedMarkers = []; 


Ohh leaflet... I know that for version 11 has more features then for version 10 of outsystems.

What i did in the past with leaflet, was to use the component for outsystems 11, and recreate it for 10, if its your version.  
This becouse for Outsystems 11, leaflet component is more well wrapped with outsystems actions and events and structures. So if you want to display multiple points, its just add them to your strutcture, and you dont have to struggle with JS.


with best regards,
Slavi Popov




Slavi Popov wrote:

Ohh leaflet... I know that for version 11 has more features then for version 10 of outsystems.

What i did in the past with leaflet, was to use the component for outsystems 11, and recreate it for 10, if its your version.  
This becouse for Outsystems 11, leaflet component is more well wrapped with outsystems actions and events and structures. So if you want to display multiple points, its just add them to your strutcture, and you dont have to struggle with JS.


with best regards,
Slavi Popov




Thanks for pointing out this point. I will definitely upgrade it to 11.

As this question was for list in JS I am marking Denial's ans as a solution. 

Thank you so much for the help

-Prajakta


Daniël Kuhlmann wrote:

If you get the error  "TypeError: Cannot read property 'push' of undefined" then I expect that the object onto which you want to push something is not an array. .push only works for arrays in javascript (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push)

Maybe you forgot to initialize the selectedMarkers variable like:

var selectedMarkers = []; 


Yes I forgot to do that. It was my mistake. Now it is working fine.

Thank you so much for the help!!

-Prajakta


Hi Daniël Kuhlmann 

Hi Prajakta,

OutSystems does not allow you to define an output parameter of data type list.

Only basic data types are allowed.

So you would need to serialize the javascript list to a JSON string using javascript JSON.stringify()

Then in OutSystems you can use the JSON Deserialize action to convert the JSON string to a structure of type list.

Regards,

Daniel

 Based on solution, I've applied that in my project. Therefore, I got an issue with asynchronous in Javascript.

You can see the image below, I use JS to upload image to third party. After that, they have a callback function to return the image url. However, the problem here is the callback hasn't return the data yet. It takes time to upload then return response. And at the time for waiting response. ListAppendAll is run. Therefore, it didn't have any value. Please help me on this situation. Thanks !

 

Hi Duy,

Next time create your own post for your own problem. Now you added your question to an existing thread that has already a reply marked as a solution. If someone gives you a reply it cannot be marked as solution, and thereby it will be less visible to other OutSystems community members.

To answer your question, please read the following document on the usage of the $resolve() and $reject() functions:
https://success.outsystems.com/Documentation/11/Extensibility_and_Integration/JavaScript/Extend_Your_Mobile_and_Reactive_Apps_Using_JavaScript/Defining_Asynchronous_JavaScript_Code 

Regards,

Daniel