2017-12-18 250 views
0

那么,我有一个用户模型,需要实现customToJSON并删除被返回的json对象的密码。 当我把“responseType”作为“json”放在“exit”中时,一切都很好,并且密码从响应中取出。但是,根据发送responseType为空的终端中的消息,将不推荐使用responseType:“json”,但不调用customToJSON。任何人都可以帮我理解这个吗?Sails 1.0,如何从Action2调用customToJSON?

这是型号代码:

[...] 

    attributes: { 

    name: { 
     type: 'string', 
     required: true, 
    }, 

    email: { 
     type: 'string', 
     required: true, 
    }, 

    password: { 
     type: 'string', 
     minLength: 6, 
     required: true, 
    }, 
}, 

customToJSON: function() { 
    return _.omit(this, ['password']); 
}, 
[...] 

这是动作代码:

module.exports = { 

friedlyName: 'Users List', 

description: 'User list -> all users', 

exits: { 
    success: { 

    } 

}, 

fn: async (inputs, exits) => { 

    var users = await User.find(); 

    return exits.success(users); 

} 

} 

是,如果你把 “的responseType: 'JSON'”:消息

json响应类型将在即将发布的版本中弃用。

回答

0

我定义内部API /响应夹自定义响应请使用``(标准),而不是(即,从success出口除去responseType)。

因此,例如... 200 OK响应创建API /响应/ ok.js

,然后1动作控制器的方法在这里面加入退出:我简单的API的

exits: { 
    success: { 
     description: 'Returns ok response from api/responses/ok.js', 
     responseType: 'ok' 
    } 
}, ... 

例/responses/ok.js

module.exports = function ok(data) { 
    return this.res.status(200).json(data); 
}; 

可以为每个状态创建应答/退出,你需要和以后很容易保持出口...我有回应的:错误请求,SERVERERROR,FO rbidden,not found和未授权

+0

我喜欢这个解决方案。但从我的理解来看,这似乎是一个仍然没有解决的问题,因为框架应该提供本地的。你可以在 https://trello.com/c/5SkpUlhI/402-make-customtojson-work-with-actions2 中看到这个,代表官方帆队js的trello js - > v1 –

+0

是的,你是对的...但仍然,我更喜欢这个...代码是更加maintanable然后:) – hlozancic