1
votes

I am using the mongoose-unique-validator plugin to validate the username in my schema, and I am using Postman to test my routes. When I test the register route of my app with a username I know has already been registered, Postman will get hung up on the POST request, but the mongoose validation error will be present in my console. My question is, how do I get Postman to display the mongoose validation error message as JSON so I can then use the error message in my app?

const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');
const Schema = mongoose.Schema;

const UserSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    username: {
        type: String,
        required: true,
        unique: true,
        uniqueCaseInsensitive: true
    },
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    date: {
        type: Date,
        default: Date.now()
    }
});

UserSchema.plugin(uniqueValidator, { message: 'Someone already has that username'});

module.exports = User = mongoose.model('users', UserSchema);
1

1 Answers

0
votes

Try this out,

`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);
    }) 
}))`