Page 1 of 2

Query Appery.io database with localstorage variable

Posted: Tue Jul 09, 2013 2:02 am
by Randy Mayo

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.


Query Appery.io database with localstorage variable

Posted: Tue Jul 09, 2013 2:32 am
by Alena Prykhodko

Query Appery.io database with localstorage variable

Posted: Thu Oct 31, 2013 6:22 pm
by Brock Gunter-Smith

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.


Query Appery.io database with localstorage variable

Posted: Thu Oct 31, 2013 10:09 pm
by Brock Gunter-Smith

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! :-)


Query Appery.io database with localstorage variable

Posted: Thu Oct 31, 2013 10:16 pm
by Maryna Brodina

Hello! Thank you for sharing information!


Query Appery.io database with localstorage variable

Posted: Wed Jan 22, 2014 7:18 pm
by Victor Fagerström

This helped me with my project, thank you!


Query Appery.io database with localstorage variable

Posted: Wed Mar 12, 2014 6:21 pm
by Mike Maughan

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


Query Appery.io database with localstorage variable

Posted: Wed Mar 12, 2014 6:46 pm
by Victor Fagerström
  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


Query Appery.io database with localstorage variable

Posted: Wed Mar 12, 2014 8:18 pm
by Mike Maughan

Thank you! That helped immensely.


Query Appery.io database with localstorage variable

Posted: Sat Jul 12, 2014 9:42 am
by Joseph6975630

Used Victor's suggestion and it worked! Thanks