0

I dont know why i am getting a validator error response. I have tried to debug as much as possible but i cant see to find where the problem is. I have used the exact names i have used in my schema but still im getting the error in both postman and rest client My server.js looks like this:

const app = express()
const mongoose = require('mongoose')
const dotenv= require('dotenv')
const routesUrls=require('./routes/routes')
const cors = require('cors')
 
dotenv.config()
mongoose.connect(process.env.DATABASE_ACCESS, () =>console.log("Database Connected"))

app.use(express.json())
app.use(cors())
app.use('/app', routesUrls)
app.listen(4000, () => console.log("server is up and running"))

My routes.js

const router = express.Router()
const signupTemplateCopy = require('../models/SignUpModels')

router.post('/signup/', (request,response) =>{
const SignedupUser =  new signupTemplateCopy({
            fullname:request.body.fullname,
            username:request.body.username,
            email:request.body.email,
            password:request.body.password   
})

        SignedupUser.save().then(data =>{
        response.json(data)})
        .catch(error =>{
            response.json(error)
        })
})
module.exports = router ```

The Schema looks like this

```const mongoose = require('mongoose')
const signupTemplate = new mongoose.Schema({

    fullname:{
        type:String,
        required:true
    },
    username:{
        type:String,
        required:true
    },
    email:{
        type:String,
        required:true

    },
    password:{ 
        type:String,
        required:true
    },
    date:{
        type:Date,
        default:Date.now
    }
})
module.exports = mongoose.model('mytable', signupTemplate)

The error

  "errors": {
    "fullname": {
      "name": "ValidatorError",
      "message": "Path `fullname` is required.",
      "properties": {
        "message": "Path `fullname` is required.",
        "type": "required",
        "path": "fullname"
      },
      "kind": "required",
      "path": "fullname"
    },
    "username": {
      "name": "ValidatorError",
      "message": "Path `username` is required.",
      "properties": {
        "message": "Path `username` is required.",
        "type": "required",
        "path": "username"
      },
      "kind": "required",
      "path": "username"
    },
    "email": {
      "name": "ValidatorError",
      "message": "Path `email` is required.",
      "properties": {
        "message": "Path `email` is required.",
        "type": "required",
        "path": "email"
      },
      "kind": "required",
      "path": "email"
    },
    "password": {
      "name": "ValidatorError",
      "message": "Path `password` is required.",
      "properties": {
        "message": "Path `password` is required.",
        "type": "required",
        "path": "password"
      },
      "kind": "required",
      "path": "password"
    }
  },```
  "_message": "mytable validation failed",
  "message": "mytable validation failed: fullname: Path `fullname` is required., username: Path `username` is required., email: Path `email` is required., password: Path `password` is required."
}



3
  • how do you test your API? In what format are you trying to send your data? Commented Nov 30, 2020 at 17:02
  • it seems ur not putting in the required data Commented Nov 30, 2020 at 17:13
  • In Json.. What is the required data? Commented Nov 30, 2020 at 17:16

2 Answers 2

1

make sure u have used the exact names in the POSTMANT and in the route.js check the caps that was my issue

const router = express.Router();
const signUpTemplateCopy = require("../models/signupmodels");

router.post("/signup", (request, response) => {
 const signedUpUser = new signUpTemplateCopy({
   fullName: request.body.fullName,
   userName: request.body.userName,
   Email: request.body.Email,
   password: request.body.password,
 });
 signedUpUser.save().then((data) => {
   response.json(data).catch((error) => {
     response.json(error);
   });
 });
});

module.exports = router;
Sign up to request clarification or add additional context in comments.

Comments

0
router.post('/signup/', (request,response) => {
const { fullname, username, email, password } = request.body
const SignedupUser =  new signupTemplateCopy()

SignedupUser.fullname = fullname;
SignedupUser.username = username;
SignedupUser.email = email;
SignedupUser.password = password;

SignedupUser.save()
.then(data => {
    response.json(data)
})
.catch(error => {
        response.json(error)
}) )}

You could give this a try.. I am fairly new to mongoDb, express, nodeJs, but I think a good practise is to put these kind of functions in a controller file.

Also, if using postman, make sure you set the headers, Content-Type -> Application/JSON. And that your raw body to send shoud look something like this...

{ "fullname": "John Doe", "username": "johndoe123", "email": "[email protected]", "password": "123456 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.