I want to modify a property value before returning it to the client. I've seen examples on doing this here:
http://docs.strongloop.com/display/public/LB/Remote+hooks
However, this time, I don't want to just delete a property. I actually want to change it.
This is my model definition:
{
"name": "Person",
"plural": "people",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
...
"name": {
"type": [
"LocalizedProperty"
],
"required": true
}
...
"acls": [],
"methods": []
}
So as you can see, a person's name is made of an array of models (LocalizedProperty). What I want here is to not return a array of this models, but instead some String.
This is what I'm using:
Person.afterRemote('find', function (ctx, people, next) {
ctx.result = _.map(people, function (model) {
model.name = "John Smith The Second";
});
next();
});
But every time I do that, I get an error that says something like this:
"message": "could not create List from JSON string: \"John Smith The Second\"",
MORE INFO If I clone the object who's property I'm trying to replace, it works great, but cloning each object in this array is very very extremely inefficient. So this is no an option.
Also, I printed the prototype, console.log(model.prototype)
and it prints nothing.
答案 0 :(得分:0)
此处此处:
ctx.result = _.map(people, function (model) {
model.name = "John Smith The Second";
});
根据格式化远程方法响应here
的文档进行查看尝试这样的格式:
module.exports = function(app) {
var remotes = app.remotes();
// modify all returned values
remotes.after('**', function (ctx, next) {
ctx.result = {
data: ctx.result
};
next();
});
};