JM
Posts: 0
Joined: Sat Sep 20, 2014 4:34 am

List service - How do you return only user specific data not everything in database!

Hi

I have a database collection that includes the user_id against every object.

I use a list service to return a list of objects from the database to display to the user.

The problem is that my list service returns every piece of data in the database rather than only data belonging to the logged in user.

I do not want to use the query service and there is alot of code down stream against my list service that i don't want to have to re do as its taken me serveral months and this is the last challenge to sort so i need my solution to work against the LIST service.

I have tried variations of the following in the before send mapping but have had no luck.
//var whereObject = {"User": value};
//return JSON.stringify(whereObject);

return '{User: "' + value + '" }';

// var whereObject = {
// User_id: {"$inQuery": {User: "' + value + '" } }
// };
// return JSON.stringify(whereObject);

But nothing seems to filter out the results by user

Please advise

Image Image

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

List service - How do you return only user specific data not everything in database!

JM - the only difference between the 'list' service and the query service for a data source (please check this yourself) - when you go to the settings and request tabs - I believe - is that the query service has an additional one or more parameters on the query string tab - on the request page. The additional parameter is a where clause.

So - on your list service - you can go to the request page (click 'request' on the left side) - and then at the top of the service - click 'query string'. Under the box labeled "new paramater name' - enter the word 'where' in that box - and click add (make sure where is entered without quotes).

You can then use this service (click save of course) - with the where clause.

You would of course test your service to make sure - by going to test the service, and entering the where clause you believe you need.

Looks like by reviewing your same from above it would be something like :

{ 'user_id' : 'some_user_id_value_that_you_want_totestwithbybrowsingyourdata'} - where user_id is the name of the column in your database .

of course - before you try anything here - you'd do a backup of your source so you can easily go back to the before you made any changes.

your mapping above - you would change your LS variable to point at the where clause - and in the JS code there you would likely put something like this:

console.log( "{ 'nameofuseridfield' : '" + value + "' }" );
return "{ 'nameofuseridfield' : '" + value + "' }" ;

of course replacing nameofuseridfield with your userid field. The logging is so that you can see in the console log the where clause - and debug it until it matches the one that worked in your test above.... when you tested the data service (before going to implement in the code)...

best,

Bruce

JM
Posts: 0
Joined: Sat Sep 20, 2014 4:34 am

List service - How do you return only user specific data not everything in database!

Hi,

Thanks so much for your reply, i'm going to give it a go now and i'll let you know how i go!

JM
Posts: 0
Joined: Sat Sep 20, 2014 4:34 am

List service - How do you return only user specific data not everything in database!

Thanks Bruce!

Wow :) that was so simple in the end. Your explanation was awesome. Thank you so much for your help i'm really greatful.

Thanks again

JM

Return to “Issues”