从昨天获取数据

时间:2018-09-27 06:16:08

标签: javascript node.js momentjs

所以我只需要从前一天开始收集数据。示例:今天是2018/9/25,我需要仅在2018/9/24上获取数据。但是从下面的代码中,它花费了23到25。这是一天多的时间,而且从我需要的日期开始还花了两天。我不知道哪个代码会导致错误的结果。有人可以帮我吗?我真的很感激。

Api.js

const TO_DAYS = 4194304 * 1000 * 60 * 60 * 24; // this part that might be the cause
const ROOT_DATE = moment([2018, 3, 30]); // this part that might be the cause
const ROOT_DATE_ID = 440557948108800000; // this part that might be the cause

const DATE_ID = function(date) {
  return ROOT_DATE_ID + date.diff(ROOT_DATE, "days") * TO_DAYS;
}; // this part that might be the cause

class discordApi {
  constructor() {
    this.lock = new AsyncLock();
  }
  get(momentDate, authorId, offset = 0) {
    const url =
      config.endpoint +
      querystring.stringify({
        min_id: DATE_ID(momentDate),
        author_id: authorId
      });
    return fetch(url, {
      headers: {
        method: "GET",
        Authorization: config.auth
      }
    }).then(res => {
      // console.log(res.url);
      return res.json();
    });
  }

  async getAllData(momentDate) {
    const allData = config.targets.map(author_id =>
      this.get(momentDate, author_id)
    );
    return Promise.all(allData);
  }

index.js

 var yesterday = moment().subtract(1, "days"); // this part that might be the cause

    async function sendEmail() {
      const data = await discordApi.getAllData(yesterday);
      const unfilteredMessages = data.reduce(
        (prev, current) => [...prev, ...current.messages],
        []
      );
      const filteredMessages = unfilteredMessages.reduce((prev, current) => {
        if (prev.length === 0) {
          return [...prev, current];
        }
        const currentConversationIsDuplicated = isConversationDuplicated(
          prev[prev.length - 1],
          current
        );
        if (currentConversationIsDuplicated) {
          return prev;
        }
        ret

urn [...prev, current];
  }, []);
  const convo = await discordApi.AllConvo(filteredMessages);

  const mailOptions = {
    from: "lala@gmail.com",
    to: maillist,
    subject: "Discord-Bot Daily Data",
    html: convo
  };
  transporter.sendMail(mailOptions, function(err, info) {
    if (err) console.log(err);
    else console.log("Message Sent!");
  });
}

0 个答案:

没有答案