[英]MongoDB collection.find() query hanging
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. 查询从未完成,Chrome开发人员工具在请求中显示“待处理”。 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.
我在mongo cli中执行了相同的查询,并且有效。 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;
这是因为
if (err) res.send(err); return;
if (err) res.send(err); return;
gets evaluated as 被评估为
if (err) {
res.send(err);
}
return;
To fix the issue, consider adding some braces. 要解决此问题,请考虑添加一些花括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.