I'm learning MERN stack development and I'm making a practice app with user login/registration. I have the Node server and MongoDB up and running, and I'm able to register new users, update usernames & passwords, retrieve a list of users, and retrieve info about a single user. I'm doing this using post/get requests to localhost:4000/credentials-test/etc
with Postman (I haven't actually connected the front end yet).
I'm able to get info on a single user with the following code in my server.js file:
credsRoutes.route('/user/:id').get(function(req, res) {
let id = req.params.id;
User.findById(id, function(err, user) {
res.json(user);
});
});
I figured I'd be able to do something similar to check if a username already exists (to prevent duplicate usernames on registration or username changing), with the following code:
credsRoutes.route('/check-user/:username').get(function(req, res) {
let username = req.params.username;
User.find(username, function(err, user) {
if (!user)
res.json('User not found :(');
else
res.json('User found! Details:');
res.json(user);
});
});
But the response from localhost:4000/credentials-test/check-username/testuser
is always User not found :(
, even when the username definitely belongs to an existing user.
Any ideas why this might be happening and how I can implement a working solution?