Node.js LoopbackJS响应格式-在返回属性之前修改属性
我想在将属性值返回给客户端之前修改它。我在这里看到过这样做的例子: 但是,这一次,我不想只删除一个属性。我真的想改变它 这是我的模型定义:Node.js LoopbackJS响应格式-在返回属性之前修改属性,node.js,formatting,hook,response,loopbackjs,Node.js,Formatting,Hook,Response,Loopbackjs,我想在将属性值返回给客户端之前修改它。我在这里看到过这样做的例子: 但是,这一次,我不想只删除一个属性。我真的想改变它 这是我的模型定义: { "name": "Person", "plural": "people", "base": "PersistedModel", "idInjection": true, "options": { "validateUpsert": true }, "properties": { ... "name":
{
"name": "Person",
"plural": "people",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
...
"name": {
"type": [
"LocalizedProperty"
],
"required": true
}
...
"acls": [],
"methods": []
}
如您所见,人名由一系列模型组成(LocalizedProperty)。这里我想要的不是返回这个模型的数组,而是一些字符串
这就是我正在使用的:
Person.afterRemote('find', function (ctx, people, next) {
ctx.result = _.map(people, function (model) {
model.name = "John Smith The Second";
});
next();
});
但每次我这样做,我都会得到一个错误,上面写着这样的话:
"message": "could not create List from JSON string: \"John Smith The Second\"",
module.exports = function(app) {
var remotes = app.remotes();
// modify all returned values
remotes.after('**', function (ctx, next) {
ctx.result = {
data: ctx.result
};
next();
});
};
更多信息
如果我克隆我试图替换的对象的属性,效果很好,但是克隆这个数组中的每个对象效率非常低。所以这不是一个选择
此外,我还打印了原型,console.log(model.prototype)
,但它不打印任何内容。本节:
ctx.result = _.map(people, function (model) {
model.name = "John Smith The Second";
});
根据文档查看远程方法响应的格式
尝试以下格式:
"message": "could not create List from JSON string: \"John Smith The Second\"",
module.exports = function(app) {
var remotes = app.remotes();
// modify all returned values
remotes.after('**', function (ctx, next) {
ctx.result = {
data: ctx.result
};
next();
});
};