Michael4771079
Posts: 0
Joined: Sat Jul 21, 2012 2:03 pm

saving array in db

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

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

saving array in db

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.

Michael4771079
Posts: 0
Joined: Sat Jul 21, 2012 2:03 pm

saving array in db

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?

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

saving array in db

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

Michael4771079
Posts: 0
Joined: Sat Jul 21, 2012 2:03 pm

saving array in db

Cheers Katya,
the app is shared and is called nfb3

Oleg Danchenkov
Posts: 0
Joined: Tue Apr 30, 2013 5:51 pm

saving array in db

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)

Michael4771079
Posts: 0
Joined: Sat Jul 21, 2012 2:03 pm

saving array in db

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

Return to “Issues”