Page 1 of 1

Search a JSON array using database _id returned from REST

Posted: Tue Apr 08, 2014 8:04 pm
by Mark Bowen6778362

Hi

I think I am making a very simple error around data types - hope someone can help me tell how to convert this data to make my JSON search work

The concept is that I have a database REST service and I want users to be able to set the items and order as a subset using local storage

I have a localStorage which is turned into JSON:
{"5343197ae4b07b0d8015f66f":{"Order":1},"534319aee4b013c661e5e61f":{"Order":1}}

I am creating a local SQLite table that will only write to the table once the 'order is resolved' from the localStorage

The code is

var myList = localStorage.getItem('localOrder');
jmylist =JSON.parse(myList);
item = value._id;
var itemOrder = (jmylist[item].Order);
It returns
TypeError: 'undefined' is not an object (evaluating 'jmylist[item].Order')

If I use the following in a console it works
var itemOrder = (jmylist["5343197ae4b07b0d8015f66f"].Order);

I realise the type of the object is wrong - what do I have to do to make it work?


Search a JSON array using database _id returned from REST

Posted: Tue Apr 08, 2014 8:33 pm
by Kateryna Grynko

Hi Mark,

Where do you set a value of "value" variable?

Let's output it in codealert(value._id);/code


Search a JSON array using database _id returned from REST

Posted: Tue Apr 08, 2014 8:45 pm
by Mark Bowen6778362

Hi, Thank you.

it is being returned from a REST list service - using this tutorial http://docs.appery.io/tutorials/build...

so value._id is the id column in my database

I get 534305d6e4b013c661e5e558 for my first item - this is the correct id number

If I do typeof item - it returns as a string

Mark


Search a JSON array using database _id returned from REST

Posted: Wed Apr 09, 2014 3:09 am
by Illya Stepanov

Hi Mark,

Please modify your code with code below:
precode
var myList = localStorage.getItem('localOrder');
var jmylist =JSON.parse(myList);

//See console to what exactly in this variable. Should be object.
console.log(jmylist);

var item = value._id;

//Check this value with keys from the first log.
console.log("item = " + item);

if(!jmylist || !jmylist[item])
console.log("jmylist[item] not exists");
else
var itemOrder = (jmylist[item].Order);/code/pre
And see console for the values.

If you have a problem with it after, please provide us public link and desribe steps to reproduce the problem.

Regards.


Search a JSON array using database _id returned from REST

Posted: Wed Apr 09, 2014 1:25 pm
by Mark Bowen6778362

Thanks - that helps, the 'if exists' appears to have made my code work

As always very helpful