xman
Posts: 0
Joined: Sun Jul 06, 2014 7:05 pm

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

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????

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

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

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

xman
Posts: 0
Joined: Sun Jul 06, 2014 7:05 pm

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

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.

Anil Sagar
Posts: 0
Joined: Fri Jul 04, 2014 1:13 pm

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

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

xman
Posts: 0
Joined: Sun Jul 06, 2014 7:05 pm

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

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

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

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

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.

xman
Posts: 0
Joined: Sun Jul 06, 2014 7:05 pm

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

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!

Return to “Issues”