简体   繁体   English

React & Axios:无法在 UI 中呈现数组值,但我可以控制台记录它们

[英]React & Axios: Unable to render array values in UI but I can console.log them

I am trying to render an array of data on the UI.我正在尝试在 UI 上呈现一组数据。 So far, I have been able to console.log the array but have not been able to display it on a table.到目前为止,我已经能够对数组进行控制台记录,但无法在表格上显示它。

I am currently using Axios to retrieve the data from the back-end and am trying to render the data in Bootstrap Table.我目前正在使用 Axios 从后端检索数据,并尝试在 Bootstrap Table 中呈现数据。 My initial issue was that I needed to assign each child in the list a unique key prop.我最初的问题是我需要为列表中的每个孩子分配一个唯一的 key prop。 Now that this error is gone, I still cannot see the data in the UI.现在这个错误消失了,我仍然看不到 UI 中的数据。

Invite table component (InviteTable.js):邀请表组件(InviteTable.js):

import React, { useState, useEffect } from 'react';
import Axios from 'axios';
import BootstrapTable from 'react-bootstrap-table-next';
import PaginationFactory from 'react-bootstrap-table2-paginator';
import ToolkitProvider, {Search} from 'react-bootstrap-table2-toolkit/dist/react-bootstrap-table2-toolkit';
import Spinner from 'react-bootstrap/Spinner';

const InviteTable = () => {

    const [invites, setInvites] = useState([]);
    const [loading, setLoading] = useState(false);
    const { SearchBar } = Search;
    const url = "http://localhost:5000/api/get";

    //Define columns
    const columns = [
        { dataField: "Id", text: "Id", headerStyle: () => {return { width: "10%" };} },
        { dataField: "Code", text: "Invite Code", headerStyle: () => {return { width: "10%" };} },
        { dataField: "Recipient", text: "Recipient Email", headerStyle: () => {return { width: "35%" };}  },
        { dataField: "Created", text: "Date Sent", headerStyle: () => {return { width: "35%" };}  },
    ];

    //GET and set data
    useEffect(() => {
        Axios.get(url).then(result => {
            setInvites(result.data);
            console.log(result.data);
            setLoading(true);
        })
        .catch(function(error) {
            console.log(error);
          });
    }, []);

    return (
        <div>
            {loading ? (
                  <ToolkitProvider keyField="Id" data={invites} columns={columns} search>
                {(props) => (
                    <div>
                        <SearchBar {...props.searchProps} />
                        
                        <BootstrapTable
                        {...props.baseProps}
                        pagination={PaginationFactory()}
                        />
                    </div>
                )}
                </ToolkitProvider>                  
            ) : (
                <Spinner animation="border" />
            )}
        </div>
    )
};

export { InviteTable }

Console: Output of console.log控制台: console.log 的输出

I have resolved this issue.我已经解决了这个问题。 The problem lay in the server file that was running the query to return the invites.问题在于运行查询以返回邀请的服务器文件。

I am quite new to this so am still in the process of learning as I go.我对此很陌生,所以我仍在学习中。

Initially I was sending the entire result to the front end which was causing issues:最初我将整个结果发送到导致问题的前端:

request.query('select * from Invites;', (err, result) => {
            
            // console log error if there is one
            if (err) console.log(err);

            // send records as a response
            res.send(result);
            
        });

To resolve this, I specified that the recordset should be sent to the front end:为了解决这个问题,我指定记录集应该发送到前端:

request.query('select * from Invites;', (err, result) => {
            
            // console log error if there is one
            if (err) console.log(err);

            // send records as a response
            res.send(result.recordset);
            
        });

Here is the solution, first check your data if it's in the object then convert it into an array.这是解决方案,首先检查您的数据是否在对象中,然后将其转换为数组。 and make sure your columns' dataField name same as your database parameters并确保您的列的数据字段名称与您的数据库参数相同

const [post, setPost] = useState([]);

then(response => {
      const data = response.data;
      const arr = Object.entries(data); //convert object into array
      arr.forEach(([key, value]) => {
      setPost(oldArray => [...oldArray, value]); // Assign Data toTable 
        
      });
    })

 <BootstrapTable keyField='id' data={post} columns={columns} pagination={paginationFactory()}/>

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

相关问题 console.log React 中选中复选框值的数组 - console.log an array of checked checkbox values in React 通过对象内部的数组进行迭代,console.log可以正常工作,但是我无法使其返回react元素。 - Iterating through an array inside an object, console.log working but I can not get it to return react elements. 我可以 console.log 我的数组,但是当附加到 div 时,它的值显示为未定义 - I can console.log my array but it's values shows up as undefined when appended to a div 无法在React UI中呈现过滤后的数组值 - Unable to render filtered array values in react UI 我如何在console.log中如何发送#form值? - how can i console.log how the #form values will be sent? react-native排序console.log中的Render和Constructor - Render and Constructor in react-native ordering console.log 什么是react组件显示的render方法中的console.log? - What is console.log in the render method of a react component showing? 如何显示 console.log? - How can I display console.log? React 过滤后的值数组未在下拉列表中更新,但实际上已在 console.log 中更新 - React filtered array of values is not getting updated in the dropdown,however is in fact updated in console.log React 在控制台中显示值但不渲染它们 - React shows values in console but does not render them
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM