简体   繁体   English

在我的反应原生项目中显示 object 和 axios 给出未定义

[英]Display object with axios in my react native project gives undefined

I was trying to get request data from PHPMyAdmin and then output it on my react native project, and I use response.data.username, and yet I get undefined.我试图从 PHPMyAdmin 然后 output 在我的 react native 项目中获取请求数据,我使用 response.data.username,但我没有定义。

 const getInfo = () => { axios.get("https://luxrealest.com/includes/user_registration.php").then( (response) => { console.log(response.username); setDataInfo(response.data.username + " " + response.data.email); }) } getInfo()
 <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.24.0/axios.min.js" integrity="sha512-u9akINsQsAkG9xjc1cnGF4zw5TFDwkxuc9vUp5dltDWYCSmyd0meygbvgXrlc/z7/o4a19Fb5V0OUE58J7dcyw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

Expected预期的

Object {
  "": Object {
    "email": "myke@myke.com",
    "id": "11",
    "username": "myke",
  },
  "1": Object {
    "email": "aisosa.erharuyi11@gmail.com",
    "id": "12",
    "username": "General.Bigman",
  },
  "2": Object {
    "email": "amiatorjay@gmail.com",
    "id": "13",
    "username": "Jaycee",
  },
}

Given the shape of your data you need to iterate over this object like this:鉴于数据的形状,您需要像这样迭代此 object:

 for (let key in response.data) {
     setDataInfo(response.data[key].username + " " + response.data[key].email);
 }

This of course will call setDataInfo 3 times for your data.这当然会为您的数据调用setDataInfo 3 次。 If you want only a specific user you will need to add additional logic.如果您只需要特定用户,则需要添加其他逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM