用户登录不在node.js和mlab中工作

问题描述 投票:0回答:1

我在mlab中有用户数据库(用户名,密码),并希望通过检查数据库中存在的用户进行登录,但它总是抛出错误的用户名/密码,即使它存在于数据库中也是如此。

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);
  })
      };
node.js mongodb react-native mlab
1个回答
0
投票

server.js

FindOne查询返回一个对象,您正在检查它的长度,该长度始终为false。

试试这个:

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"})
    }
})


    })
© www.soinside.com 2019 - 2024. All rights reserved.