Randy Mayo
Posts: 0
Joined: Tue Jul 09, 2013 2:02 am

Query Appery.io database with localstorage variable

I am new at this so what I am asking may seem trivial to most. I am trying to retrieve a database record using the value of the record "_id" stored in a localstorage variable called "CurrentItemCommentID". I have verified the proper _id number is being stored and I can enter it in the where clause manually using:

return '{"_id": {"$regex":"51d857c0e4b0b7b459fc4d3f" }}';

I want to do the same thing using the localstorage variable called "CurrentItemCommentID" which has that exact same hex decimal string stored in it.

Brock Gunter-Smith
Posts: 0
Joined: Thu Oct 31, 2013 6:22 pm

Query Appery.io database with localstorage variable

Alena (or anyone else), I'm challenged by the same thing. In the datasource page I have a parameter setup like where={"incident_id": Appery('currentIncident') } It doesn't work, but I'd like it so that when I invoke the service it uses the local storage variable currentIncident to limit the returned results.

Brock Gunter-Smith
Posts: 0
Joined: Thu Oct 31, 2013 6:22 pm

Query Appery.io database with localstorage variable

Ok, as usually happens, I dug in deeper and found what I needed. I'll post my solution here for others who happen to be looking for the same thing.

  1. I added a property called "where" to the datasource for listing entries from the database.

  2. I added JS to the "where" property.

  3. The JS is as follows, which basically just grabs the variable from the local storage that I'm looking for, and contructs the proper JSON parameters for the where clause, or returns nothing if the variable is not valid or set and we shouldn't filter the query:

    var incidentID = localStorage.getItem('currentIncidentID');
    var conds = [];
    if (incidentID '10000000' ) { conds.push('"incident_id":"'+incidentID+'"'); }
    else { return; }
    return '{' + conds.join(',') + '}';

    Voila. Pretty simple. I'm surprised there isn't a more graphical way to integrate where clauses and sorting parameters in queries...but I suppose that would be my recommendation for future updates in Appery! :-)

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Query Appery.io database with localstorage variable

Hello! Thank you for sharing information!

Victor Fagerström
Posts: 0
Joined: Sun Dec 01, 2013 1:03 pm

Query Appery.io database with localstorage variable

This helped me with my project, thank you!

Mike Maughan
Posts: 0
Joined: Wed Feb 19, 2014 10:55 pm

Query Appery.io database with localstorage variable

I'm trying to filter the DB list service response with a "where" request parameter, but I am also unable to reference a localStorage variable in the "where" request parameter field.

I'm new to all this, so could someone please outline what to do in smaller steps, similar to what you would do in your documentation to help guide me through it. I've tried the steps listed by user Brock Gunter-Smith, however I'm just not quite able to make it all work (I'm new to this). So a more granular explanation would be very helpful!

Thanks!

Mike

Victor Fagerström
Posts: 0
Joined: Sun Dec 01, 2013 1:03 pm

Query Appery.io database with localstorage variable

  1. create a localStorage variable inside mapping of a service.

    1. save something in the localStorage variable.

    2. inside mapping of query service; drag the localStorage variable to the where parameter.

    3. click add JS on where parameter.

    4. write JS. the localStorage variable you dragged to the where parameter will be referred to in your JS as "value" (without " ").
      6.
      example 1 will give response of all usernames with the value of your localStorage variable:
      return '{"username": "' + value + '"}';

      example 2, result same as 1, written differently:
      var myOwnWhereParam = {"username": "' + value + '"};

      return myOwnWhereParam;

      example 3, result same as 1 and 2, approached differently:
      instead of mapping a localStorage variable to the where parameter you can access it via JS:

      var LSVariable = localStorage.getItem('nameOfVariable');

      return '{"_id": "' + LSVariable + '"}';

      I hope this helped you. Good luck!
      V

Mike Maughan
Posts: 0
Joined: Wed Feb 19, 2014 10:55 pm

Query Appery.io database with localstorage variable

Thank you! That helped immensely.

Joseph6975630
Posts: 0
Joined: Wed Jul 09, 2014 1:49 pm

Query Appery.io database with localstorage variable

Used Victor's suggestion and it worked! Thanks

Return to “Issues”