Page 1 of 1

Getting length of an array stored in a cell in the database collection

Posted: Tue Jul 22, 2014 4:47 am
by xman

I have a DB collection "X", where there is a column "C" of the type "array". I'm reading the value in the column "C" by indexing it with _id and "where" query service.

The result I am getting back is correct (debugged using an alert). This is what I get returned (each array element is a code):

["834cd", "dke9rwe", "dii94", "ckdi9e"]

...this is exactly what is stored in the DB cell of concern, so this is right!

Now, I want to calculate the length of these value. So what I do is first store the result from Query response into local variable called arrLSV (by mapping the response to it). Then, to form a formatted string I'v defined a Javascript called formattedJSV to which a string from response is matched. Since I wanted to display this string with array length post-pended, I've tried to do the following in the "add JS" for "formattedJSV":

var arr = localStorage.getItem("arrLSV");
var str = value + " total: " + arr.length;
Appery("someFormattedLabel").text(str);

This is not working. I get null returned back as size. When I try to print things out due to debug, the arrLSV prints out fine until I don't add the arr.length code. When arr.length code is added I started getting weired results (sometimes not consistent with the last time).

How can I calculate the arrLSV's length properly????


Getting length of an array stored in a cell in the database collection

Posted: Tue Jul 22, 2014 9:18 am
by Kateryna Grynko

Hi,

It's better to obtain array length when running service. Add the following code to service Success event:prevar len = data.length;
alert(len);/pre


Getting length of an array stored in a cell in the database collection

Posted: Tue Jul 22, 2014 4:13 pm
by xman

Hi Katya,

I did not understand your suggestion fully - is it based on an assumption that server side code is running and returning back "data"?

What I have is not server code but a Database Query Service, that returns an array of information about a particular userID. The array element of interest here is a cell in the DB which itself is an array (this is what I need the length for).

I made some change to the code above that I put in posting:

var arr = JSON.stringify(localStorage.getItem("arrLSV"));

getting the arr.length now seems to not return "NULL" but a value. I need to verify this further why it's returning incorrect arr length for an array which has only 1 element. The other array I tried had 4 elements, and this code returned current length for that array.


Getting length of an array stored in a cell in the database collection

Posted: Tue Jul 22, 2014 5:15 pm
by Anil Sagar

Are u using read service in one and query service in the other?


Getting length of an array stored in a cell in the database collection

Posted: Tue Jul 22, 2014 5:16 pm
by xman

No, I just have the Query service that returns the user details from the DB. I then run JS on the response.


Getting length of an array stored in a cell in the database collection

Posted: Wed Jul 23, 2014 2:40 am
by Yurii Orishchuk

Hi Xman,

In case you have described in first message you need to use following code:

pre

//Note: you should replace "someArray" with your LSV name.

var arrayText = localStorage.getItem("someArray");

//Set default array if here is not items.
if(!arrayText)
arrayText = "[]";

var arrayObject = JSON.parse(arrayText);

var length = arrayObject.length;

alert("length = " + length);

/pre

Regards.


Getting length of an array stored in a cell in the database collection

Posted: Wed Jul 23, 2014 10:41 pm
by xman

Thanks Yurii, I got it working in the $ mapping of the response already. However, it would not run if any array in the loop over the DB was empty. This is where your suggesting of setting default array if there is no item comes very handy!

Appreciated!