【问题标题】:Uncaught (in promise) TypeError: (intermediate value).toLocalDateString is not a function at Stats.js:62Uncaught (in promise) TypeError: (intermediate value).toLocalDateString is not a function at Stats.js:62
【发布时间】:2021-08-13 08:36:31
【问题描述】:
function Stats() {
  const [customerList, setCustomerList] = useState([]); //store all that information of the database in a list
  //make an axios request to get information from database
  const getCustomers = () => {
    Axios.get("http://localhost:3001/customers").then((response) => {
      //console.log("successfully retrieved customers list from database");
      console.log(response.data);
      setCustomerList(response.data);
    });
  };

  {/*}
    const [currentTime, setCurrentTime] = useState(1);
  
    useEffect(() => {
      fetch("/time")
        .then((res) => res.json())
        .then((data) => {
          setCurrentTime(data.time);
        });
    }, []);
  

    useEffect(() => {
      fetch("/time")
        .then((res) => res.json())
        .then((data) => {
          const dateStr = new Date(data.time).toLocalDateString('en-CA');
          const timeStr = new Date(data.time).toLocalTimeString();
          const dateTime = `${dateStr} ${timeStr}`;
          setCurrentTime(dateTime);
        });
    }, []);
    */}
    
  return (
    <div>
      <Navbar />
      <div className="container">
      <h1>Dashboard</h1>
      <button onClick={getCustomers}>Show Dashboard</button>
      </div>
      <table className="customertable">
        <thead>
        <tr>
          <th>S/N</th>
          <th>Customer Name</th>
          <th>Customer Email</th>
          <th>Counts of Visit</th>
          <th>Latest Time of Visit</th>
          <th>Contacted?</th>
        </tr>
        </thead>
        <tbody>
        {customerList.map((val, key) => {
          const dateStr = new Date(val.latest_time_of_visit).toLocalDateString('en-CA');
          const timeStr = new Date(val.latest_time_of_visit).toLocalTimeString();
          const dateTime = `${dateStr} ${timeStr}`;
          return (
            <tr>
              <td>{val.ID}</td>
              <td>{val.name}</td>
              <td>{val.email}</td>
              <td>{val.counts_of_visit}</td>
              <td>{dateTime}</td>
              <td>{val.contacted}</td>
            </tr>
          );
        },)}
        </tbody>
      </table>
    </div>
  );
}

export default Stats;

这是我使用的代码,错误发生在: 当我单击"Show Dashboard" 按钮时会发生此错误,该按钮在单击时调用getCustomer 函数,是否有人对此错误有任何解决方案?当我单击按钮时。我省略了导入语句,但如果您需要它们,请告诉我或发表评论!感谢您的帮助!

const dateStr = new Date(val.latest_time_of_visit).toLocalDateString('en-CA');

【问题讨论】:

  • 数组中最初没有值。试试customerList.length &amp;&amp; customerList.map(...
  • 它拼写为 locale 但不是本地,因为您可以指定区域代码,例如 'en-CA'

标签: javascript node.js reactjs compiler-errors frontend


【解决方案1】:

toLocalDateString --> toLocaleDateString

toLocalTimeString --> toLocaleTimeString

【讨论】:

  • 谢谢它的工作我的个人资料上还有一个问题你认为你能帮忙吗?
猜你喜欢
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
  • 2017-04-13
  • 2020-03-17
  • 2020-01-06
相关资源
最近更新 更多