Skip to content

Changes made in afterRemote hooks are discarded #162

Closed

Description

@bajtos

Consider the following hook:

User.afterRemote('**', function(ctx, result, next) {
  delete result.password;
  result.newProp = 'new-value';
  result.email = 'modified:' + result.email;
  next();
});

// Result of POST /users
{
  password: '$2a$10$6Ck1zg3NgKLrsRHelaV9MuwwbOFbfxVnSccTcWvODRAC16c85/r0i',
  email: 'modified:foo@foo.com',
  credentials: [],
  challenges: [],
  id: 1 
}

In other words, password property was not removed, newProp was not added, only the modification of an existing property was applied.

Partial workaround based on the internal details of ModelBaseClass:

user.afterRemote('**', function(ctx, result, next) {
  delete result.__data.password;
  result.__data.newProp = 'new-value';
  result.email = 'modified:' + result.email;
  next();
});

// Result of POST /users
{
  password: null,
  email: 'modified:foo@foo.com',
  credentials: [],
  challenges: [],
  id: 1,
  newProp: 'new-value'
}

Related and/or affected issues: #160, #161.

/cc @Raymond @Ritchie

Activity

  1. bajtos commented on Jan 30, 2014

    @bajtos
    MemberAuthor

    I was contemplating two different approaches how to fix this.

    1. Modify ModelBaseClass.toObject() to include all own enumerable properties in the result.

    2. Modify SharedMethod.toResult to perform a structured clone (*) of all return values when composing the final result object. This way the "after" callback will work with plain data objects instead of full Models, which makes it easier to add/delete/modify data. On the other hand, such solution will not allow "after" callback to call any methods on the result objects (e.g. in order to fetch more data from the DB and add it to the response).

      (*) The term "structured clone" comes from HML5 spec and is implemented e.g. by lodash.cloneDeep. Our implementation would have to modify the algorithm to clone only public properties (REST: User response includes hashed password #160).

    @raymondfeng @ritch Is there a reason why ModelBaseClass.toObject() enumerates only properties defined in the model schema and (optionally) in __data? The consequences of 2. are huge and I am not entirely sure if 2. is a good idea at all...

  2. raymondfeng commented on Jan 30, 2014

    @raymondfeng
    Member

    I ran into the same issue before. The data source juggler use __data as the internal representation of the property values and control the access through getter/setter functions. If you see a property on the instance directly, for example,

    myObject.x = 'x';

    if x is a predefined property for the model, the setter will add it to __data. Otherwise, it won't be intercepted.

    IMO, for models with strict !== true, the owner properties should be copied during toObject().

  3. bajtos commented on Feb 4, 2014

    @bajtos
    MemberAuthor

    Cross-posting from loopback-datasource-juggler#67.

    There are two kinds of extra data that a developer may want to add:

    • An existing property created by model relations, e.g. AccessToken.user. There should be a way how to tell the AccessToken instance that it should include the cached value of "user" relation in the JSON.

      // inside User.login
      accessToken.user(user);
      if (include === 'user') {
        accessToken.user.public = true; // made up API
      accessToken.toJSON().user === user.toJson(); // true 
    • A new property that is not defined in the schema.

      model.set('foo', 'bar');
      model.foo === 'bar'; // true
      model.toJson().foo === 'bar'; // true
  4. raymondfeng commented on Feb 28, 2014

    @raymondfeng
    Member

    Fixed by the PR above.

  5. ulion commented on Mar 7, 2015

    @ulion
    Contributor

    It seems "delete result.field" still not affect the final response?

  6. bajtos commented on Mar 10, 2015

    @bajtos
    MemberAuthor

    AFAIK, you need to call result.unsetAttribute('field') for that.

  7. ulion commented on Mar 10, 2015

    @ulion
    Contributor

    Thx for the tip, hope it can be put on the remote hook doc.

  8. bajtos commented on Mar 11, 2015

    @bajtos
    MemberAuthor

    Thx for the tip, hope it can be put on the remote hook doc.

    /cc @crandmck

  9. sachinhub commented on Nov 24, 2015

    @sachinhub

    I have a remote hook as follows -

    Model.afterRemote ('**', function (ctx, result, next){
       console.log ('afterRemote hook called.');
    
       next ();
    });
    

    This hook gets called when GET is invoked from /explorer. However when a Model.find ({}) is executed from unit test case, the above hook is not getting called.

    Any inputs on why remote hooks are not getting called when find is executed in unit tests?

  10. darior87 commented on Dec 16, 2015

    @darior87

    Have this problem on remote hook intercepting a relationship get called.

    Band.afterRemote('**.__get__proposals',function(ctx,inst,next) {
          //i.e. inst = {};
          inst.concert(function(err, concert){
              inst = {concert : concert};
              console.log(concert);//outputs the concert object correctly
              next(null,inst);//the same with next();
            }
          });
    }

    when I'm getting this relationship I have the {} json as response object when I'm expecting {concert:concert}

  11. darior87 commented on Dec 16, 2015

    @darior87

    it works after replacing the result in the context

    ctx.result = {concert:concert};

    before the next(); to be called

  12. kartsims commented on May 29, 2017

    @kartsims

    Just found out this old issue and definetely 👍 to @ulion : the unsetAttribute method should go in the doc regarding afterRemote hook

  13. bajtos commented on May 29, 2017

    @bajtos
    MemberAuthor

    Just found out this old issue and definetely 👍 to @ulion : the unsetAttribute method should go in the doc regarding afterRemote hook

    @kartsims would you like to contribute that improvement yourself? Here are the instructions to get you started: http://loopback.io/doc/en/contrib/doc-contrib.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions