简体   繁体   English

类型错误:moment__WEBPACK_IMPORTED_MODULE_3___default(...)(...).calendar(...).sort 不是函数

[英]TypeError: moment__WEBPACK_IMPORTED_MODULE_3___default(...)(...).calendar(...).sort is not a function

I fetch the data from this link .我从这个链接获取数据。

I want to show the data based on a recent date.我想显示基于最近日期的数据。 I used the map method to render the data then try to use sort them based on date.我使用 map 方法来呈现数据,然后尝试使用基于日期对它们进行排序。 But I got an error.但我有一个错误。 I used the moment package to making the date organized.我使用 moment 包来组织日期。 Sorry for my English, I don't know if express my feeling right.对不起我的英语,我不知道表达我的感受是否正确。

This is my React's component setup这是我的 React 组件设置

import React, { useEffect, useState } from "react";
import axios from "axios";
import moment from "moment";


const Events = () => {
  const [events, setEvents] = useState([]);

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

  const fetchingData = () => {
    axios
      .get("/events")
      .then(response => {
        console.log("response", response);
        setEvents(response.data.data);
      }) 
      .catch(err => console.log(err));
  };

  //do console.log
  console.log("events", events);

  return (
    <div>
      {events.map(list => {
        return (
          <div>
            <div className="card">
              <div className="carddd">
                <div className="card_image">
                  <ul>
                    <li>
                      {moment(list.event_dates.starting_day.toString())
                        .calendar()
                        .sort((a, b) => { // I try sort date in here.
                          return a - b;
                        })}
                    </li>
                  </ul>

                  <div>
                    {list.description.images.map(img => {
                      return (
                        <img
                          src={img.url}
                          alt="jj"
                          style={{ width: "250px", height: "250px" }}
                        />
                      );
                    })}
                  </div>
                </div>
              </div>
            </div>
          </div>
        );
      })}
    </div>
  );
};

export default Events;

Screenshot below下面的截图

在此处输入图片说明

Going by Moment's documentation on .calendar() here it returns a string: https://momentjs.com/docs/#/displaying/calendar-time/通过 Moment 关于.calendar()的文档,它返回一个字符串: https : .calendar()

Javascript strings do not have a .sort() function, so you're trying to call a function that doesn't exist which is causing your error. Javascript 字符串没有 .sort() 函数,因此您试图调用一个不存在的函数,这会导致您的错误。

You will want to sort the dates before converting them to strings, so you'll want to run sort on your events array before you do the map.您需要在将日期转换为字符串之前对其进行排序,因此您需要在绘制地图之前对events数组运行排序。

Solution edit:解决方案编辑:

So your response comes back and you have the events in state under events .所以,你的反应回来,你必须下状态的事件events

Before you render, you could reduce your events data down to just being dates, keyed using the ID of each event.在渲染之前,您可以事件数据缩减为日期,使用每个事件的 ID 键入。

const eventDates = events.reduce((acc, event) => {
    acc[event.id] = {
        startDate: moment(event.event_dates.starting_day),
        endDate: moment(event.event_dates.ending_day),
        // You could also add more info in here, like the name or location.
    }
    return acc;
}, {});

Then use Object.keys() to get an array (which can be sorted, objects can't.) of the keys.然后使用Object.keys()获取键的数组(可以排序,对象不能。)。 Then sort that array by accessing the values in eventDates, using the keys in the array, like so.然后通过访问 eventDates 中的值来对该数组进行排序,使用数组中的键,就像这样。

let sortedByStartDate = Object.keys(eventDates);
sortedByStartDate.sort(
    (a, b) => eventDates[a].startDate - eventDates[b].startDate
);

The keys will now be sorted by the start dates.键现在将按开始日期排序。 ( By the way, to sort moment objects, you can just use the - operator on them ). 顺便说一句,要对矩对象进行排序,您只需对它们使用 - 运算符即可)。

Now when you are rendering you want to map through the sortedByStartDate array, using the keys to access values in eventDates (you may also want to add additional information in here in the reduce to get access to, instead of just dates).现在,当您渲染时,您希望通过sortedByStartDate数组进行映射,使用键访问eventDates值(您可能还想在 reduce 中添加其他信息以访问,而不仅仅是日期)。

sortedByStartDate.map(
    eventKey => <li>{eventDates[eventKey].startDate.calendar()}</li>
);

暂无
暂无

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

相关问题 TypeError:react__WEBPACK_IMPORTED_MODULE_6 ___ default.a.useState不是一个函数 - TypeError: react__WEBPACK_IMPORTED_MODULE_6___default.a.useState is not a function 类型错误:标记为__WEBPACK_IMPORTED_MODULE_3___default(...) 不是函数 - TypeError: marked__WEBPACK_IMPORTED_MODULE_3___default(...) is not a function "类型错误:_fire__WEBPACK_IMPORTED_MODULE_1__.default.auth 不是函数" - TypeError: _fire__WEBPACK_IMPORTED_MODULE_1__.default.auth is not a function 类型错误:_models_Products__WEBPACK_IMPORTED_MODULE_1__.default.find 不是函数 - TypeError: _models_Products__WEBPACK_IMPORTED_MODULE_1__.default.find is not a function 类型错误:_fire__WEBPACK_IMPORTED_MODULE_11__.default.collection 不是 function - TypeError: _fire__WEBPACK_IMPORTED_MODULE_11__.default.collection is not a function 类型错误:_firebase__WEBPACK_IMPORTED_MODULE_9__.default.collection 不是 function - TypeError: _firebase__WEBPACK_IMPORTED_MODULE_9__.default.collection is not a function 类型错误:firebase_app__WEBPACK_IMPORTED_MODULE_0___default.a.analytics 不是 function - TypeError: firebase_app__WEBPACK_IMPORTED_MODULE_0___default.a.analytics is not a function 类型错误:Webpack 导入的模块不是 function - TypeError: Webpack imported module is not a function TypeError:__ WWEPACK_IMPORTED_MODULE_0_react ___ default.a.createRef不是函数 - TypeError: __WEBPACK_IMPORTED_MODULE_0_react___default.a.createRef is not a function 类型错误:_firebase__WEBPACK_IMPORTED_MODULE_2__.default.collection 不是函数 - TypeError: _firebase__WEBPACK_IMPORTED_MODULE_2__.default.collection is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM