Page 1 of 1

If/Then in Where parameter

Posted: Tue Jul 15, 2014 2:40 am
by Peter Viglietta

This is a simple question, I know, I just don't know enough Javascript yet.. can someone help me out??

I have a page in my dating site app where users view posts on a message board, posted by other users. When the user starts a session, the first things they do are

1) Choose their city (set a local storage variable "CityId")
2) Choose whether they're a man seeking a woman, woman seeking a man, etc. (Set local storage variable "PreferenceType", which is either m4w, 'w4m' etc)

Then they click View Posts. When they get to the View Posts screen, the Posts Query Service runs and returns all rows that match their CityId and Preference LS variables:

var CityId = localStorage.getItem("CityId");
var PreferenceType = localStorage.getItem("PreferenceType");
var whereObject = {"CityId": CityId, "Preference": PreferenceType };
return JSON.stringify(whereObject);

But this is wrong, because if you indicated that you are a man seeking women, (value of local storage variable "PreferenceType" is 'm4w'), then the Where parameter above makes it so that the ViewPosts page shows you Posts from other men seeking women. I need to change the 'var whereObject' part so that it has an If/Then condition, so that if your local storage variable "PreferenceType" is equal to 'm4w', it returns every row from the Posts collection where Preference is equal to 'w4m'. And vice versa- if your local storage variable "PreferenceType" is w4m, then it needs to return every row from Posts collection where Preference is m4w.

Can anyone tell me how to make that happen? Any help is much appreciated.


If/Then in Where parameter

Posted: Tue Jul 15, 2014 4:55 am
by Yurii Orishchuk

Hi Peter,

Please use following code:

pre

//Here is convert mapping. Please add other converts you need...
var PreferenceTypeMapping = {
"w4m": "m4w",
"m4w": "w4m"
};

var CityId = localStorage.getItem("CityId");
var PreferenceType = localStorage.getItem("PreferenceType");

//Converting with mapping.
PreferenceType = PreferenceTypeMapping[PreferenceType];

var whereObject = {"CityId": CityId, "Preference": PreferenceType };
return JSON.stringify(whereObject);

/pre

Regards.