在nodejs请求中解析JSON

时间:2018-05-17 09:42:52

标签: json node.js parsing

我对NodeJs中的请求有点疑问我想解析我的JSON,但我想要显示包含该消息的 msg.errors.email.message 关于我的堕落的错误。

我的错误:

throw er; // Unhandled 'error' event
^

TypeError: Cannot read property 'errors' of undefined

代码nodejs:

if (err) {
   return res.status(500).json({ msg: err.msg.errors.email.message });
}

Retour json *

{
"msg": {
    "errors": {
        "email": {
            "message": "Is not a valid e-mail.",
            "name": "ValidatorError",
            "properties": {
                "message": "Is not a valid e-mail.",
                "type": "user defined",
                "path": "email",
                "value": "f@f"
            },
            "kind": "user defined",
            "path": "email",
            "value": "f@f",
            "$isValidatorError": true
        },
        "password": {
            "message": "Password needs to have at least one lower case, one uppercase, one number, one special character, and must be at least 8 characters but no more than 35.",
            "name": "ValidatorError",
            "properties": {
                "message": "Password needs to have at least one lower case, one uppercase, one number, one special character, and must be at least 8 characters but no more than 35.",
                "type": "user defined",
                "path": "password",
                "value": "dede-DSQds"
            },
            "kind": "user defined",
            "path": "password",
            "value": "dede-DSQds",
            "$isValidatorError": true
        }
    },
    "_message": "users validation failed",
    "message": "users validation failed: email: Is not a valid e-mail., password: Password needs to have at least one lower case, one uppercase, one number, one special character, and must be at least 8 characters but no more than 35.",
    "name": "ValidationError"
}

}

我想要显示

msg.errors.email.message

1 个答案:

答案 0 :(得分:0)

我想你只需要回复那个?

if (err) {
   return res.status(500).json({ msg: err.msg.errors.email.message });
}

希望它有所帮助。