Start a new topic
Answered

Javascript SDK modify request body or params when fetching data

Hey guys.


Been looking around and can't find an answer. 


Is it possible add parameters to the body{} or params{} of the request when fetching data (using stream.subscribe) using javascript sdk? 


Want to pull params out at PreFetch 


Cheers

Phil


Best Answer
Phil,

With what I understood of the question, you can build a query using the $kinvey.Query() and add your parameters to it.

In your prefetch, you can read these query params, use the specific parameter sent for your own decision making and remove it from the query, and then let the remaining query execute.

 

Thanks,

Pranav

Kinvey


Phil,

Yes, can you give me an example of what you want to achieve?

 

Thanks,

Pranav

Kinvey

Awesome


I want to send an object containing session specific information to catch and use in preFetch.


Use Case:

Collection that contains reports

User can open a report (which retrieves the data). As they edit it in the browser the app will periodically update the server with the data. 

To prevent another user opening the same report and doing the same thing, or the same user having it open in other windows and creating crazy versioning issues all other reports are set to read only. To this I apply a "lock" to the data which will set their window to read only unless it matches the session info.  What I wanted to do is check if the doc has the "lock" flag on preFetch and if not set the lock using the session info passed in params or body. 


Hope that makes sense?



That and handy to know how to send params with fetch and save requests. 

Phil,

Yes, you should be able to pass params into requests and access them in the preFetch() hook using request.params variable.


Please take a look at 

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


Let me know if you need additional help.


Thanks,

Pranav

Kinvey

 

Hi Pranav


Sorry I should have explained better. Accessing them with BL is fine. I wasn't sure how to send them using the Javascript SDK


i.e. 


If I am fetching using: 

var stream = dataStore.findById('entity-id');
stream.subscribe(function onNext(entity) {
  // ...
}, function onError(error) {
  // ...
}, function onComplete() {
  // ...
});

 

How do I pass in params?


Thanks for your help. Sorry if I am missing something obvious 

Answer
Phil,

With what I understood of the question, you can build a query using the $kinvey.Query() and add your parameters to it.

In your prefetch, you can read these query params, use the specific parameter sent for your own decision making and remove it from the query, and then let the remaining query execute.

 

Thanks,

Pranav

Kinvey

Login or Signup to post a comment