0

Here is my user schema:

const mongoose=require('mongoose');
const userSchema=new mongoose.Schema({
username:{
    type: String,
    required: true,
    unique: true,
    minlength: 3,
},
password:{
    type: String,
    required: true,
    minlength: 8
}
})
const User=mongoose.model('User', userSchema);
module.exports = User;

And here is my users router:

const router = require('express').Router();
const bodyParser = require('body-parser')
let User= require('../models/userSchema');
router.route('/').get((req,res)=>{
User.find()
.then(users => res.json(users))
.catch(err => res.status(400).json('Error:'+err));
});
router.route('/add').post((req,res)=>{
const username=req.body.username;
const password=req.body.password;
const newUser=new User({username, password});
newUser.save()
.then(()=>res.json('User added!'))
.catch((err)=>res.status(400).json('Error:'+err));
});
module.exports=router;

Whenever I post this in insomnia or postman, I get an error Cannot POST /users/add even though I'm successfully connected to server and to MongoDB

{
"username":"John",
"password":"password1234"
}
Eya Dkhil
  • 13
  • 3
  • Are you trying to post the field values to a localhost URL? – David R Mar 30 '22 at 14:58
  • Yes, I'm connected to port 5000 and I'm trying to post into http://localhost:5000/users/add – Eya Dkhil Mar 30 '22 at 15:00
  • Just check if you are behind any proxy, in such case try turning off the "SSL Certificate Verification" in POSTMAN under => `File -> Settings -> General -> SSL Certificate Verification` – David R Mar 30 '22 at 15:41
  • are you able to GET users? I know you probably don't have any yet, but it should show `[]`, or are you getting an error? also what does your server file look like where you are requiring the userRouter? – Dylan L. Mar 30 '22 at 19:29

0 Answers0