如何使用reactjs从后端在表中检索数据?

时间:2019-06-01 13:22:04

标签: reactjs

我想使用axios从django后端检索所有用户名。用户在控制台中显示的位置,但所有详细信息数据:(用户名电子邮件...),标题,令牌...。  所以我想限制它只是获取数据或仅在控制台中显示数据  之后,我想知道如何将用户名放在表或列表中

这是我的检索功能:

 const RetrievUsers=()=>{
      axios.get('http://127.0.0.1:8000/user/list') 
      .then(data =>
      console.log(data)
     )

    .catch(err=>console.log(err))

    } 

后端的数据如下:

"username": "pol",
        "password": "**********************",
        "nom": "a",
        "prenom": "a",
        "email": "a@gmail.com",
        "residence": "po",
        "phone": "po"

控制台:

{data: Array(2), status: code, statusText: " ", headers: {…}, config: {…}, …}config: {adapter: ƒ, transformRequest: {…}, transformResponse: {…}, @@@}

/I need only this part 
data:Array(2)
0: email: "a@gmail.com"nom:"a "password: "***pbkd g=" phone: "po"
prenom: "a"residence: "po"username: "pol"__proto__: Object
1: email: "a@gmail.com" nom: "a" password: "*****A4=" phone: "a"
prenom: "a"residence: "pass"username: "a"__proto__: Object
// 
length: 2__proto__: Array(0)headers: { }request: XMLHttpRequest {onreadystatechange:   …}status:

1 个答案:

答案 0 :(得分:0)

要过滤用户名,只需尝试像这样

console.log(data.data[0].username);

要遍历数据,请尝试

data.data.forEach(function(element) {
  console.log(element.username);
});