loopback icon indicating copy to clipboard operation
loopback copied to clipboard

No access token available in context for "loaded" operation hook

Open hyperd opened this issue 9 years ago • 7 comments

Hi. I'm using the "loaded" operation hook to do some final manipulations before retrieving one of my models. The problem I'm having is that I want to get the access token for the current context but the options property is empty.

Should the current context be fully loaded automatically or is it that I'm missing an step here?

Thanks.

Sample code:

MyModel.observe('loaded', function(context, callback) {
        if (context.isNewInstance) {
            return callback();
        }
       
       //var accessToken = context.options.accessToken;
      //context.options is an empty object
}

hyperd avatar May 12 '17 03:05 hyperd

Doing a bit more research seems that this is the case for GET requests but not for POST/PUT. Customizing what options returns solves the issue: https://loopback.io/doc/en/lb3/Using-current-context.html#access-the-context-from-operation-hooks

hyperd avatar May 12 '17 03:05 hyperd

Dispite all this I believe it would be nice if access token were available by default in the options object.

hyperd avatar May 12 '17 13:05 hyperd

cc @bajtos

ebarault avatar May 12 '17 13:05 ebarault

@hyperd I believe the options parameter should be set for GET requests too. Could you please share the code you are invoking via GET? Do you perhaps have a custom method that does not forward the options argument to the call of a built-in PersistedModel method?

bajtos avatar May 22 '17 14:05 bajtos

@bajtos Sorry for the delay in replying. I think I didn't explain well. The options parameter is set correctly for GET requests. The problem is with PUT or POST requests. I'll set up a sample project to repro this

hyperd avatar Jun 07 '17 01:06 hyperd

+1

RubanMaster avatar Jun 30 '17 05:06 RubanMaster

@hyperd @bajtos , I have also the same issue. but when it gets empty I am not able to figure it out clearly. BTW I am trying to pass data throughout in the application & access it. I have to hit GET request multiple time according to my application need. e.g., Person details and their Order data. Just before the person details it gets "empty". I think so.

uds214125 avatar Apr 18 '18 18:04 uds214125