I am using Swagger UI to send post request in order to register users to the mongoDB. I am getting the following error
- on the backend terminal: data and salt arguments required
- On the UI console: POST http://localhost:3002/register net::ERR_CONNECTION_RESET
My app is already using CORS:
app.use(cors({
origin: ['http://localhost:5173', 'http://localhost:3002'],
methods: ["GET", "POST", "UPDATE", "DELETE", "PUT" , "PATCH"],
credentials: true
}))
My code for the registration (including swagger) is as follow:
/**
* @swagger
* tags:
* name: Employee Model
* description: api to manage employees Model
*/
/**
* @swagger
* components:
* schemas:
* EmployeeModel:
* type: object
* properties:
* name:
* type: string
* email:
* type: string
* password:
* type: string
*/
/**
* @swagger
* /register:
* post:
* summary: This api is used to register new employee
* tags: [Employee Model]
* description: This api is used to register new employee
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: array
* items:
* $ref: '#/components/schemas/EmployeeModel'
* responses:
* 200:
* description: This api is used to post data to mongodb (register employee)
*/
// Addiong single user to the databse (to Mongo - document)
// Register Users for login
app.post('/register', (req, red) => {
const {name, email, password} = req.body
bcrypt.hash(password, 10)
.then(hash => {
EmployeeModel.create({name, email, password: hash})
.then(employee => res.json(employee))
.catch(err => red.json(err))
}).catch(err => console.log(err.message))
})
Is there something wrong on my code ? Any advices how to improve the code to successfully insert the data ?