簡體   English   中英

我正在嘗試將從 API 收到的數據放入表中,但沒有顯示任何內容

[英]I'm trying to put the data I'm receiving from my API into a table but nothing is displayed

I'm getting data from my API, when I make a console.log or JSON.stringify in the API data it shows without problems but when I pass the data in a table with the map, simply nothing is presented in the table. 在此處輸入圖像描述 .

const [users, setUsers] = useState([]);
    const loadUser = () => {
        getUsers().then(data => {
            if(data.error) {
                console.log(data.error)
            }else{
              
                setUsers(data)
            }
        })
    }
const inforUsers = () => {
        return(
            <Fragment>
            <table className="table table-bordered mb-5">
                <thead className="thead-dark">
                    <tr>
                        <th scope="col">Id</th>
                        <th scope="col">Nome</th>
                        <th scope="col">Email</th>
                        <th scope="col">role</th>
                        <th scope="col">createdAt</th>
                    </tr>
                </thead>
                <tbody scope="row">
                {Object.keys(users).map((values, key) => (
                    <tr key={key}>
                        <td>
                            {values._id}
                        </td>
                        <td>
                            {values.name}
                        </td>
                        <td>
                            {values.email}
                        </td>
                        <td>
                            {values.role === 1? 'Admin' : 'Simples User'}
                        </td>
                        <td>
                            {values.createdAt}
                        </td>
                    </tr>
                ))} 
                </tbody>
            </table>
            </Fragment>
            )
    }

我認為您對手頭的數據感到困惑。 密鑰是每個 object 的 id,因此如果您想要該數據,您應該通過從 Object.keys 獲得的每個密鑰/ID 訪問users Object.keys 一個簡單的例子:

{Object.keys(users).map(id => (
   {users[id]._id}
))}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM