You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Changes made in afterRemote hooks are discarded #162
User.afterRemote('**',function(ctx,result,next){deleteresult.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){deleteresult.__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'}
I was contemplating two different approaches how to fix this.
Modify ModelBaseClass.toObject() to include all own enumerable properties in the result.
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).
@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...
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().
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.loginaccessToken.user(user);if(include==='user'){accessToken.user.public=true;// made up APIaccessToken.toJSON().user===user.toJson();// true
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?
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 correctlynext(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}
Activity
bajtos commentedon Jan 30, 2014
I was contemplating two different approaches how to fix this.
Modify
ModelBaseClass.toObject()to include all own enumerable properties in the result.Modify
SharedMethod.toResultto perform a structured clone (*) of all return values when composing the finalresultobject. 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...raymondfeng commentedon Jan 30, 2014
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().
Make sure own properties are copied by toObject for non-strict mode
bajtos commentedon Feb 4, 2014
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.A new property that is not defined in the schema.
raymondfeng commentedon Feb 28, 2014
Fixed by the PR above.
ulion commentedon Mar 7, 2015
It seems "delete result.field" still not affect the final response?
bajtos commentedon Mar 10, 2015
AFAIK, you need to call
result.unsetAttribute('field')for that.ulion commentedon Mar 10, 2015
Thx for the tip, hope it can be put on the remote hook doc.
bajtos commentedon Mar 11, 2015
/cc @crandmck
sachinhub commentedon Nov 24, 2015
I have a remote hook as follows -
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?
darior87 commentedon Dec 16, 2015
Have this problem on remote hook intercepting a relationship get called.
when I'm getting this relationship I have the
{}json as response object when I'm expecting{concert:concert}darior87 commentedon Dec 16, 2015
it works after replacing the result in the context
ctx.result = {concert:concert};
before the next(); to be called
kartsims commentedon May 29, 2017
Just found out this old issue and definetely 👍 to @ulion : the
unsetAttributemethod should go in the doc regardingafterRemotehookbajtos commentedon May 29, 2017
@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