如何将Mongoose验证错误传递给JSON for Postman

时间:2019-06-12 19:13:29

标签: mongodb error-handling postman mongoose-schema

我正在使用mongoose-unique-validator插件来验证模式中的用户名,并且正在使用Postman测试我的路线。当我使用我知道已经注册过的用户名测试我的应用程序的注册路由时,Postman会挂在POST请求上,但是我的控制台中会出现猫鼬验证错误。我的问题是,如何让Postman将猫鼬验证错误消息显示为JSON,以便随后可以在应用程序中使用该错误消息?

var data = [ { id: 5, name: "Luke Skywalker", pilotingScore: 98, shootingScore: 56, isForceUser: true, }, { id: 82, name: "Sabine Wren", pilotingScore: 73, shootingScore: 99, isForceUser: false, skills:{ 'skill1':'vision', 'skill2':'strength' } }, { id: 22, name: "Zeb Orellios", pilotingScore: 20, shootingScore: 59, isForceUser: false, }, { id: 15, name: "Ezra Bridger", pilotingScore: 43, shootingScore: 67, isForceUser: true, skills:{ 'skill1':'vision', 'skill2':'strength' } }, { id: 11, name: "Caleb Dume", pilotingScore: 71, shootingScore: 85, isForceUser: true, }, ];

let pick = (obj, fields) => Object.keys(obj)
  .reduce((r,c) => (fields.includes(c) ? r[c] = obj[c] : null, r), {})

let result = data.map(x => pick(x, ['id', 'name', 'skills']))

console.log(result)

1 个答案:

答案 0 :(得分:0)

尝试一下,

`axios.get('/user', (req, res =>{
    User user = new User(req.body);
    user.save().then(data =>{
        req.send(200).data("saved");
    }).catch(e =>{
        req.send(500).send(e.message);
    }) 
}))`