问题
I am facing an issue trying to override StrongLoop built in method for PUT request.
So in model.js file I am using:
Model.on('attached', function(){
Model.updateAttributes = function(data, id, cb){
cb(null,'This is a overridden method');
};
}
But when I call the endpoint with a PUT /api/v1/models/1 and payload this function does not get executed but the built in one. I also tried to use other function instead of updateAttributes but without any success like for example:
Model.updateAll = function([where], data, cb) {
cb(null, 'this is a overriden method');
}
Model.create = function(data, cb) {
cb(null, 'this is overriden method');
}
Thanks for helping me out.
回答1:
Instead of overriding the method, you can disable and attach a new method to the same endpoint as follows:
Model.disableRemoteMethodByName('updateAttributes');
Model.newMethod = function(cb) {
cb(null, 'new message');
}
Model.remoteMethod('newMethod', {
returns: {
arg: 'msg'
},
http: {
verb: 'put',
path: '/'
}
});
来源:https://stackoverflow.com/questions/38848987/strongloop-overriding-put-built-in-method