【问题标题】:Reactjs- I keep getting the error: TypeError: companies.map is not a functionReactjs-我不断收到错误:TypeError:company.map 不是函数
【发布时间】:2021-09-29 15:42:15
【问题描述】:

我正在尝试使用 nodejs 和 reactjs 将 mysql 数据显示到表中。 出于某种原因,我保留了以下错误:

TypeError:company.map 不是函数

我从昨天开始就一直试图找出问题所在,但没有运气。 我什至向 useState 传递了一个空数组,但这不起作用。

如果有人可以帮助我找出我的代码存在的问题,那就太好了。

我的代码如下:

import { useEffect, useState } from "react";
import { Button, Container, Row, Table } from "react-bootstrap";
import axios from "axios";

const AllCompanies = () => {
  const [companies, setCompanies] = useState([]);

  useEffect(() => {
    getAllCompanies();
  }, []);

  const getAllCompanies = async () => {
    const response = await axios.get("http://localhost:8000/companymst");
    console.log(response.data);
    setCompanies(response.data);
  };

  return (
    <div className="App">
      <div className="auth-wrapper">
        <div className="auth-inner">
          <Container>
            <Row>
              <Table striped bordered hover size="sm">
                <thead>
                  <tr>
                    <th>CompanyCode</th>
                    <th>CompanyName</th>
                    <th>Address1</th>
                    <th>Address2</th>
                    <th>PoBox</th>
                    <th>City</th>
                    <th>Province</th>
                    <th>Country</th>
                    <th>Phone</th>
                    <th>Fax</th>
                    <th>Email</th>
                    <th>RegistrationNo</th>
                    <th>VatNo</th>
                    <th>PinNo</th>
                    <th>BranchNo</th>
                    <th>BranchHq</th>
                    <th>StartDate</th>
                    <th>EndDate</th>
                    <th>Current</th>
                    <th>RunDate</th>
                    <th>DateCreated</th>
                    <th>UserID</th>
                    <th>LocationID</th>
                    <th colSpan="2">Actions</th>
                  </tr>
                </thead>
                <tbody>
                  {companies.map((company) => {
                    return (
                      <tr>
                        <td>{company.CompanyCode}</td>
                        <td>{company.CompanyName}</td>
                        <td>{company.Address1}</td>
                        <td>{company.Address2}</td>
                        <td>{company.PoBox}</td>
                        <td>{company.City}</td>
                        <td>{company.Province}</td>
                        <td>{company.Province}</td>
                        <td>{company.Country}</td>
                        <td>{company.Phone}</td>
                        <td>{company.Fax}</td>
                        <td>{company.Email}</td>
                        <td>{company.RegistrationNo}</td>
                        <td>{company.VatNo}</td>
                        <td>{company.PinNo}</td>
                        <td>{company.BranchNo}</td>
                        <td>{company.BranchHq}</td>
                        <td>{company.StartDate}</td>
                        <td>{company.EndDate}</td>
                        <td>{company.Current}</td>
                        <td>{company.RunDate}</td>
                        <td>{company.DateCreated}</td>
                        <td>{company.UserID}</td>
                        <td>{company.LocationID}</td>
                        <td>
                          <Button variant="info">Edit</Button>
                        </td>
                        <td>
                          <Button variant="danger">Delete</Button>
                        </td>
                      </tr>
                    );
                  })}
                </tbody>
              </Table>
            </Row>
          </Container>
        </div>
      </div>
    </div>
  );
};

export default AllCompanies;

【问题讨论】:

  • 我敢打赌response.data 是一个对象而不是一个数组。控制台记录它

标签: javascript mysql node.js reactjs


【解决方案1】:

正如@captain-yossarian 所说,可能是你得到了反对,为了避免这个问题,你可以像下面那样做。

{companies?.length > 0 && companies.map((company) => {
   return '';
})}

【讨论】:

  • 这确实使错误消失了,但现在手头的问题是我的表中没有显示数据
  • 如果你得到对象数组然后它会显示记录。你的数据应该是这样的 [{name:"john",email:"abc@gmail.com",message:"Hello"},{name:"Doe",email:"bcd@gmail.com",message: “你好 123”}]
  • result: Array(1) 0: {CmpnyCode: "2", CmpnyName: "Comapny1", Address1: "Address1", Address2: "Address2 at sth", PoBox: "359", ... } length: 1 proto: Array(0) proto: Object 这是我在 console.log(response.data) 时得到的
  • {company.CompanyCode} {company.CompanyName} 无论您获得响应的任何键以及您尝试访问的任何键都是不同的。请在地图功能中记录“公司”。
  • 无变化。仍然得到一张空白表
猜你喜欢
  • 2016-08-08
  • 1970-01-01
  • 2019-08-20
  • 1970-01-01
  • 2017-02-07
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 1970-01-01
相关资源
最近更新 更多