I am trying to create a new user in mongodb and looking for existing user
but even if a am sending request with new user details it is still showing that User alreadyExists
export const signUp = async (req, res) => {
const {email, password, username, roomid} = req.body;
try {
console.log(req.body);
let existingUser = User.findOne({email});
console.log(existingUser);
if (existingUser) res.status(400).json({message: 'User already exists!!'});
const hashedPassword = await bcrypt.hash(password, 12);
const result = await User.create({
email,
password: hashedPassword,
username,
roomid,
});
const token = jwt.sign(
{email: existingUser.email, id: existingUser._id},
'test',
{expiresIn: '1hr'}
);
res.status(201).json({result, token});
} catch (err) {
console.log(err);
}
};
Help Please
Someone just solve the problem of new user creation
3
Answers
Try this
Change line, because even after this line the program continues to run.
To:
You need to
return
key.Try changing the code with the Else method so:
Note: Don’t forget to turn the server off and then on again.