Hello,
How can I fix the MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
when I added the JSON for http://127.0.0.1:5000/v1/auth/register
in postman?
This is the JSON:
{
"name": "john",
"password": "secret",
"email": "john@gmail.com"
}
Here’s my User model:
import mongoose from 'mongoose';
import validator from 'validator';
const UserSchema = new mongoose.Schema({
role: {
type: String,
trim: true,
maxlength: 20,
default: 'admin',
},
name: {
type: String,
required: [true, 'Please provide name'],
minlength: 3,
maxlength: 20,
trim: true,
},
email: {
type: String,
required: [true, 'Please provide email'],
validate: {
validator: validator.isEmail,
message: 'Please provide a valid email',
},
unique: true,
},
password: {
type: String,
required: [true, 'Please provide password'],
minlength: 6,
select: false,
},
lastName: {
type: String,
trim: true,
maxlength: 20,
default: 'lastName',
},
location: {
type: String,
trim: true,
maxlength: 20,
default: 'Tacloban City',
},
});
export default mongoose.model('User', UserSchema);
Then my authController.js
import User from '../models/User.js';
const register = async (req, res) => {
try {
const user = await User.create(req.body);
res.status(201).json({user});
} catch (error) {
res.status(500).json({msg: 'there was an error'});
}
};
const login = async (req, res) => {
res.send('login user');
};
const updateUser = async (req, res) => {
res.send('updateUser');
};
export{register, login, updateUser};