I am trying to make a query to find a user by username like so:
userRouter.get('/user/:user_username', function(req, res) {
console.log("GET request to '/user/" + req.params.user_username + "'");
User.find({ usernmame: req.params.user_username }, function(err, user) {
if (err) res.send(err); return;
res.json(user);
});
});
The query never completes and Chrome dev tools is showing 'pending' on the request. It's definitely going to that route because it prints the console message I logged at the start. I executed the same query in the mongo cli and it works. I tried logging messages in the callback body, but it never gets to that point. I'm at a loss as to what to do at this point.
It's because if (err) res.send(err); return; gets evaluated as
if (err) {
res.send(err);
}
return;
To fix the issue, consider adding some braces.