Page 1 of 3

How to JSON?

Posted: Sun Jun 01, 2014 6:10 pm
by Aris Miliotis

Hey I am scouring these questions and the tutorials and I am coming short to something that should be simple, probably my bad on not being a programmer or something....
So here's my question: I have a query to my database, I managed to do that, and I get back, in the test environment this array with [] and in it are smaller arrays with {} and all that... I am guessing that is a JSON object. How to I get my data from it?
To start with I'd like an array to contain the returned database ids on localStorage...
...


How to JSON?

Posted: Sun Jun 01, 2014 9:36 pm
by Yurii Orishchuk

Hi Aris.

Sorry but currently i can not figure out what exactly not work.

To be clear please provide us information about your problem. Perhaps screen shots. And describe them.

But here is common things code:

precode

//This is an object.
var goalObject = {"hello": "world"};

//Get JSON string from the the object.
var goalObjectJSONStrgin = JSON.stringify(goalObject);

//Store object JSON string to the LSV:
localStorage.setItem("objectJSONString1", goalObjectJSONStrgin);

//Get object JSON string from the LSV:
var restoredJSONString = localStorage.getItem("objectJSONString1");

//Convert object JSON string to the object.
var restoredJSONObject = JSON.parse(restoredJSONString);

/code/pre

Regards


How to JSON?

Posted: Mon Jun 02, 2014 3:56 pm
by Aris Miliotis

OK this is what I have... I run a query on my database and in the test environment I get as a response something like the following, with ... meaning more of the same.
How do I go from this place to having two arrays. one with all the returned _ids and one with all the locations? Once I have this data in arrays I am going to do some JS on them...

[
{
"id":"538b8c7ae4b0d8c87ceb7e60",
"location":[
-76.5477214,
39.1522171
],
"acl":{
"*":{
"write":true,
"read":true
}
},
"createdAt":"2014-06-01 20:26:34.003",
"_updatedAt":"2014-06-01 20:26:34.003"
},
...
]


How to JSON?

Posted: Tue Jun 03, 2014 1:57 am
by Yurii Orishchuk

Hi Aris.

Please clarify, perhaps with screenshots, what exactly you currently have and what is not working.

If you have Appery.io DB service and whant to access this response like an object you can:

1 Open goal page.

2 Navigate to the "Data" tab. http://prntscr.com/3p5i6u/direct

3 Create datasource with service you need.

4 Open "Events" bottom tab. http://prntscr.com/3p5ijp/direct

5 Select datasource created in 3rd step and add "Success" event with type JS and fill it with following code: http://prntscr.com/3p5l6z/direct

pre

console.log(data)

for(var i = 0; i < data.length; i++){
alert(data["_id"]);

Code: Select all

 //Here you can use code you need. 

};

/pre

That's all.

Regards.


How to JSON?

Posted: Tue Jun 03, 2014 6:43 pm
by Aris Miliotis

Thank you, but I'll trouble you more :)
I've been asking things about appery all over this forum, and mostly I am getting redirects to the help pages, which I read thoroughly many times and couldn't figure what's wrong. You were the first responder to give me step by step so I'll throw all my questions at you and see what happens...

First of the code you gave me has errors :( missing semi-colon and some alerts which I don't know what to do with... Second, the console just reports an error, so I think the query isn't running properly...

So here's the whole situation:
I am making an app that stores my user's location. I have this working, I can see the entries in the database, I made.

I am also able to take the data from the geolocation service and pass it to the map component and the default marker component, so my map looks fine.

Now begin the problems, I want to send a query to my database and get a return all the "other" users that are close to my current user. If I put the following in the test section of my query { "location" : { "$within" : { "$box" : [[-78, 37],[-75, 40]] } } } I get response data in the response. Other geoposition related queries I tried from the help files, fail even at the test. The radius one and others...

Now the numbers in the query should be different for each user based on their location, so I need to do that with JS, so I added in the JS section of my "where" variable the following: return { "location" : { "$within" : { "$box" : [[-78, 37],[-75, 40]] } } }; with the intention to alter the numbers before the "return", but I want to get this working for now...
Now the response of this service I map to a variable in localStorage I call "near_by" I just took the $[] of the response and dragged it to the variable. I need to access in this variable the "_id"s returned by the query and their locations. I need a way to get inside this "near_by" variable, as I have more data in there, which is the meat of my application...

Then I need more map markers to show these extra people. I have made the default marker component as an additional component and dragged a couple on the map, and clicked their "break apart component" button, so now I see them in the componet drop down menu of the design events, but I can't see them to click on them and edit their behaviors...

Lets get this down and we take it from there...
thank you for all your help.
.a


How to JSON?

Posted: Wed Jun 04, 2014 8:32 am
by Evgene Karachevtsev

Hello Aris,

Values are represented as strings in local storage variables. Thus, you should use JSON.parse() function to restore an object from local storage, use following code:

codevar nearObj = JSON&#46;parse(localStorage&#46;getItem("near_by")); /code

Now you can get data from this object accordingly to JavaScript syntax.
Please, look at our Google Maps tutorials. Here you can find how to manage your map component: http://devcenter.appery.io/tutorials/...
http://devcenter.appery.io/tutorials/...


How to JSON?

Posted: Wed Jun 04, 2014 7:05 pm
by Aris Miliotis

Thank you this is what I was looking for. Now I see it was too simple to find it on my own :(


How to JSON?

Posted: Wed Jun 04, 2014 7:10 pm
by Aris Miliotis

Another question... if I am binding a delete_item service on startpage UNLOAD and I am testing in my browser, will I see the item deleted on the database when I close the browser tab that has my app running?


How to JSON?

Posted: Thu Jun 05, 2014 7:10 am
by Evgene Karachevtsev

Hello Aris,

You may check this - please use event "unload" or "before unload".


How to JSON?

Posted: Thu Jun 05, 2014 3:53 pm
by Aris Miliotis

I tried both of those and nothing.
I pass the _id to the service and run it on UNLOAD or before Unload, but the item persists in the database... I am thinking it has something to do with running it in the browser or something, the app doesn't really unload or something....????