Axios返回未定义的数据吗?

时间:2019-03-20 14:23:51

标签: javascript reactjs express axios

Axios返回未定义的数据。 我正在尝试从axios发布请求中获取数据,但它始终返回未定义或返回firstname。是因为数据类型不匹配吗? 这是我尝试打印用户数据时得到的响应[i] {id:1,   名:“ Leanne”,   姓氏:“ Gram”,   密码:“ 123”,   电话:“ 9474211442”}

服务器端

const userdatas = [{
  id: 1,
  firstname: "Leanne",
  lastname: "Gram",
  password: "123",
  phone: "9474211442"
}]

app.post('/login', (req, res) => {
  let logindetail = {
    firstname: req.body.firstname,
    password: req.body.password
  }
  logindetail.firstname = logindetail.firstname.replace(/['"]+/g, '')
  logindetail.password = logindetail.password.replace(/['"]+/g, '')
  console.log(logindetail.firstname)
  var count = Object.keys(userdatas).length;
  for (var i = 0; i < count; i++) {
    if (logindetail.firstname == userdatas[i].firstname && logindetail.password == userdatas[i].password) {
      res.send(userdatas[i]);
      console.log("Hello");
    }
  }
});

客户端

handleSubmit = (e) => {
  alert(this.state.Firstname)
  e.preventDefault();
  axios.post('http://localhost:3001/login', {
    lastname: this.state.Lastname,
    firstname: this.state.Firstname,
    password: this.state.password,
    phoneno: this.state.phoneno
  }).then(res => alert(res.data.lastname))
}

0 个答案:

没有答案