i have users database(username,password) in mlab and want to login by checking that user existing in database but it always throws alert that wrong username/passwd even it is existing in database.
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 query returns an object and you are checking it's length which will always be false.
try this:
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"})
}
})
})