MongoDB + Express:如何使用db.collection(). findone()或.find()验证登录
MongoDB + Express: How to verify login credentials using db.collection().findOne() or .find()?
我有一个POST请求,用户凭据作为登录页面的对象,并像这样传递到API服务器:
loginUser(creds) {
//creds is in the form of { username: bob, password: 123 }
var request = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(creds),
}
fetch(`http://localhost:3000/api/login`, request)
.then(res => res.json())
.then(user => {
console.log(user);
console.log('Successful')
})
.catch(err => {
console.log('Error is', err)
})
},
和API服务器接收它这样:
//With .findOne()
app.post('/api/login/', function(req, res) {
console.log('Req body in login ', req.body)
db.collection('users').findOne(req.body, function(err, isMatch) {
console.log('ISMATCH IS: ' + isMatch)
if(err) {
console.log('THIS IS ERROR RESPONSE')
res.json(err)
} else {
console.log('THIS IS ISMATCH RESPONSE')
res.json(isMatch)
}
})
})
或
//With .find()
app.post('/api/login/', function(req, res) {
console.log('Req body in login ', req.body)
//console logs correctly as { username: bob, password: 123 }
db.collection('users').find(req.body).next(function(err, isMatch) {
console.log('ISMATCH IS: ' + isMatch)
if(err) {
console.log('THIS IS ERROR RESPONSE')
res.json(err)
} else {
console.log('THIS IS ISMATCH RESPONSE')
res.json(isMatch)
}
})
})
因此,通过传入的登录凭据,在API服务器内部,我想搜索我的'users'
数据库,看看是否有任何匹配传入的凭据。但在这两种情况下,isMatch
始终是null
,并且始终记录console.log('THIS IS ISMATCH RESPONSE')
,即使用户凭证与数据库中的任何凭证都不匹配。在客户端,我从来没有得到任何错误响应,总是记录console.log('Successful')
。
似乎不知道我错过了什么。我能做错什么吗?
谢谢
我建议您先找到用户,然后比较密码,例如:
db.collection('users').findOne({ username: req.body.username}, function(err, user) {
console.log('User found ');
// In case the user not found
if(err) {
console.log('THIS IS ERROR RESPONSE')
res.json(err)
}
if (user && user.password === req.body.password){
console.log('User and password is correct')
res.json(user);
} else {
console.log("Credentials wrong");
res.json({data: "Login invalid"});
}
});
应该在。findone()方法中使用$和运算符。
工作的例子:db.collection('users').findOne(
{ $and: [
{ name: req.body.username.toLowerCase() },
{ password: req.body.password }
] },
(err, result) => {
if(err) {
res.status(500).send(err);
return;
}
if(!result) {
data = {
"meta": {
"status": "fail",
"message": "Login Failure: Invalid username or password"
}
};
res.status(401).send(data);
} else {
data = {
"meta": {
"status": "success",
"message": "Login success"
}
};
res.json(data);
}
});
相关文章:
- .Find()仅显示可见元素
- jQuery(this).Pparent().Pparente().Prent().find()在iPad上不起作用
- 他们是如何使用angular/jqLite find()方法按属性名称和值选择元素的?ng conf 2015
- jQuery find() Issue
- 使用jquery Find()在复选框中查找图像
- strongloop script.js run find in before delete方法:如何同步运行异步方法
- 在另一个Find(..)的回调内部查找,如何逃离回调地狱
- $(e.target).find和template.find('input').value之间有什么区别
- 为什么我得到了一个“;未定义的“;使用“”从Find()返回值;这个“;论点
- .map-find元素一旦通过AJAX加载
- MongoDB collection.find()查询挂起
- 如何在jQuery中使用find
- 无法将参数传递给mongo find集合
- Ember.js-模型find()方法中的异步调用
- 是否可以使find()和findOne()方法只返回模式字段
- .findOne 在 Iron Router Route 的数据属性中工作,但 .find 不能
- MongoDB=>findOne还是find查询下一个对象?(Meteor/React)
- 什么's更快:' find().limit(1) '或' findOne() '在MongoDB/Mongoos
- MongoDB + Express:如何使用db.collection(). findone()或.find()验证登录
- Mean.js Stack-export.read,带有findOne和find函数,在Angularjs中调用