为什么我从查找查询中得到两个响应?
Why I'm getting two responses from find query?
我正在尝试检查 MongoDb 中是否存在用户。
出于未知原因,在我发现用户存在并发送响应后,它记录我用户不存在
app.post("/login", function(req, res) {
var userName = req.body.userName;
var pass = req.body.pass;
console.log('')
console.log('Try to login:')
console.log('userName: ' + userName);
console.log('pass: ' + pass);
if(req.body.userName && req.body.pass)
{
db.collection("customers").find({first_name:userName}, function(err, docs) {
docs.each(function(err, doc)
{
if(doc) {
//console.log(doc);
console.log('User Exsists')
res.send('YES');
}
else {
console.log('User Does Not Exsists')
res.end();
}
})});
}
else
{
console.log('NO')
res.send('NO')
}
});
输出:(第一个用户不存在,第二个用户存在)
Try to login:
userName: sdlkj
pass: lkj
User Does Not Exsists
Try to login:
userName: sivan
pass: lkj
User Exsists
User Does Not Exsists
为什么在我发现该用户存在后,我得到了另一条没有找到的日志行?
我正在尝试检查 MongoDb 中是否存在用户。 出于未知原因,在我发现用户存在并发送响应后,它记录我用户不存在
app.post("/login", function(req, res) {
var userName = req.body.userName;
var pass = req.body.pass;
console.log('')
console.log('Try to login:')
console.log('userName: ' + userName);
console.log('pass: ' + pass);
if(req.body.userName && req.body.pass)
{
db.collection("customers").find({first_name:userName}, function(err, docs) {
docs.each(function(err, doc)
{
if(doc) {
//console.log(doc);
console.log('User Exsists')
res.send('YES');
}
else {
console.log('User Does Not Exsists')
res.end();
}
})});
}
else
{
console.log('NO')
res.send('NO')
}
});
输出:(第一个用户不存在,第二个用户存在)
Try to login:
userName: sdlkj
pass: lkj
User Does Not Exsists
Try to login:
userName: sivan
pass: lkj
User Exsists
User Does Not Exsists
为什么在我发现该用户存在后,我得到了另一条没有找到的日志行?