Page 1 of 1

saving array in db

Posted: Wed May 15, 2013 10:15 am
by Michael4771079

Hi Katya,
I am trying to save an array in the db,
the array contains company name and email,

I have called the 2 services "user read" and "update user"
user read on page load and update user on click
I have added the column in db and mapped correctly
I have added the js in mapping

"user read" js
codeif (value == "") {
return "[]";
}
return JSON.stringify(value,code

"update user" js
codereturn JSON.parse(localStorage.getItem("companiesArray"));code

I am able to save the details in db, but its not reading the value on load, I think I am missing something

I keep getting the following error in console
https://getsatisfaction.com/apperyio/...

app name is nfb3 and is shared with apperyio

thanks as always/code/code/code/code


saving array in db

Posted: Wed May 15, 2013 12:45 pm
by Kateryna Grynko

Hi Michael,

As I can see the error appears when sorting array. Probably, you have an incorrect data in localStorage variable. Try cleaning local storage variable value where you store the array.


saving array in db

Posted: Wed May 15, 2013 12:52 pm
by Michael4771079

Thx Katya,
I have cleared everything but the error returns after you try to rerun the app again,

the user read doesnt seem to be working either?

unsure if I am missing something?


saving array in db

Posted: Wed May 15, 2013 2:08 pm
by Kateryna Grynko

Could you please share the app or tell us the app name if it's already shared?


saving array in db

Posted: Wed May 15, 2013 2:12 pm
by Michael4771079

Cheers Katya,
the app is shared and is called nfb3


saving array in db

Posted: Wed May 15, 2013 6:19 pm
by Oleg Danchenkov

Hi Michael.
When you store array of objects to database you get this in column
code["{name=xxxxxx, email=xxxxxx@xxxxxx}","{name=xxxxxx, email=xxxxxx@xxxxxx}"]/code
This means that you store array of strings, not array of objects.
And that is why you get error https://getsatisfaction.com/apperyio/... (you don't have objects in array).
You should convert strings to array. For example on page Taxicompanies in user_read service Response in mapping from taxicompanies to companiesArray change your JS code to
precodefunction strRepresentationToObject(str) {
str = str.slice(1, -1);
var i, len, obj={}, tmp, arr = str.split(', ');
for (i = 0, len = arr&#46;length; i < len; i++) {
tmp = arr&#46;split('=');
obj[tmp[0]] = tmp[1];
}
return obj;
}
if (value == "") {
return "[]&quot
}
var i, len, newValue = [];
for (var i = 0, len = value&#46;length; i < len; i++) {
newValue&#46;push(strRepresentationToObject(value));
}
return JSON&#46;stringify(newValue);/code/pre

And btw you should set this code
codesetSelectMenuOptions(Tiggr('taxicoselectmenu'), myload('companiesArray'));/code
on restservice8 Complete event (not on the Taxicompanies Load event)


saving array in db

Posted: Wed May 15, 2013 7:45 pm
by Michael4771079

Thanks a lot Oleg,
works very well, thx for the detail above, I now have a better understanding of the difference, also errors solved!