Start a new topic

How to add filter to query in prefetch logic?

I'm using business logic to only allow fetches of a certain _acl.creator's rows (not the same user as the requestor). In the prefetch, I'm trying to modify the request query to only fetch rows with a specific _acl.creator. How do I do this? My attempt:

My pathetic attempt:

request.body._acl = function () {

this.creator = finalkey._acl.creator;

}



Also, a link for somewhere to learn more would be helpful.

Here is probably the link you are looking for:

http://devcenter.kinvey.com/html5/reference/business-logic/reference.html#request



Your preFetch function has the request object available for you to manipulate (which includes adding additional filter params).
I've seen this: http://devcenter.kinvey.com/html5/reference/business-logic/reference.html#request



but I'm not sure which part of the request to edit. How do I add a param to the query?
request.params["query"] returns my client's query, but I can't find out how to properly add a filter to it.

I.e.

request.params['query"] returns: {\"_kmd.lmt\":{\"$gt\":\"2000-01-01T00:00:00.000Z\"}}

I want to filter this query so that it is

{"_kmd.lmt":{"$gt":"2000-01-01T00:00:00.000Z"}, "_acl.creator":{"blahblahblah"}}
Your new query looks ok, so try replacing the existing query param (in request) with your new one. I'm not sure if it will take an object direct, so escape and convert it to a string. I believe now if you do request.complete() the logic will continue with your new query parameters in place.
{\"kmd.lmt\":{\"$gt\":\"2000-01-01T00:00:00.000Z\"}}

is the old query, in the format of a String

I haven't been able to make my new query, which I want to be:

{"kmd.lmt":{"$gt":"2000-01-01T00:00:00.000Z"}, "_acl.creator":{"blahblahblah"}}



I think the problem is that the request.query is not yet parsed into a dictionary or anything. Is there an easier way to go about doing this?
I solved this via parsing the request query with JSON, editing, then re-assembling the query. I would like input on rather or not this is a good practice, especially from Kinvey folks.



logger.info('Request:' + request.params["query"]);

var myq = request.params["query"],

myqobj = JSON.parse(myq);

myqobj._acl = {"creator":finalkey._acl.creator};

var myqafter = JSON.stringify(myqobj);

request.params["query"] = myqafter;

logger.info('Request query after:' + request.params["query"]);

response.continue();

Yup, this helped me two years later.  It is because request.params.query is a string, and not an object.  JSON.parse(request.params.query) took care of this.



Login or Signup to post a comment