用户登录在 node.js 和 mlab 中不起作用
user login not working in node.js and mlab
我在 mlab 中有用户数据库(用户名,密码),想通过检查数据库中是否存在该用户来登录,但它总是抛出错误的警报 username/passwd 即使它存在于数据库中。
server.js:
app.post('/check-login',(req,res)=>{
x=users.findOne({username:req.body.username,password:req.body.password},
function(err, data) {
console.log(data)
if(data.length==1)
{
res.send({"message":"success"})
}
else{
res.send({"message":"Wrong Username Or Password"})
}
})
})
login.js:
handlePress() {
console.log("Hello");
axios.post("http://192.168.43.239:8081/check-login", {
username: this.state.username, password: this.state.password
}).then(res => {
console.log(res["data"])
if(res["data"]["message"] == "success") {
alert("Login Successfully");
this.props.navigation.navigate('Home');
}
else {
alert("Wrong Username Or Password")
}
}).catch(function (error) {
console.log(error);
})
};
server.js
FindOne 查询 returns 一个对象,而您正在检查它的长度,这永远是错误的。
试试这个:
app.post('/check-login',(req,res)=>{
x=users.findOne({username:req.body.username,password:req.body.password},
function(err, data) {
console.log(data)
if(data)
{
res.send({"message":"success"})
}
else{
res.send({"message":"Wrong Username Or Password"})
}
})
})
我在 mlab 中有用户数据库(用户名,密码),想通过检查数据库中是否存在该用户来登录,但它总是抛出错误的警报 username/passwd 即使它存在于数据库中。
server.js:
app.post('/check-login',(req,res)=>{
x=users.findOne({username:req.body.username,password:req.body.password},
function(err, data) {
console.log(data)
if(data.length==1)
{
res.send({"message":"success"})
}
else{
res.send({"message":"Wrong Username Or Password"})
}
})
})
login.js:
handlePress() {
console.log("Hello");
axios.post("http://192.168.43.239:8081/check-login", {
username: this.state.username, password: this.state.password
}).then(res => {
console.log(res["data"])
if(res["data"]["message"] == "success") {
alert("Login Successfully");
this.props.navigation.navigate('Home');
}
else {
alert("Wrong Username Or Password")
}
}).catch(function (error) {
console.log(error);
})
};
server.js
FindOne 查询 returns 一个对象,而您正在检查它的长度,这永远是错误的。
试试这个:
app.post('/check-login',(req,res)=>{
x=users.findOne({username:req.body.username,password:req.body.password},
function(err, data) {
console.log(data)
if(data)
{
res.send({"message":"success"})
}
else{
res.send({"message":"Wrong Username Or Password"})
}
})
})