【问题标题】:Error: Expected an assignment or function call and instead saw an expression no-unused-expressions. How to solve it?错误:需要一个赋值或函数调用,而是看到一个表达式 no-unused-expressions。如何解决?
【发布时间】:2021-01-09 20:44:28
【问题描述】:

我实际上是在尝试将我的数据库连接到一个反应应用程序,它一直在三个不同的地方向我显示这个错误,我在下面用“>>”突出显示

 handleDBReponse(response) {
    const appointments = response;
    const today = moment().startOf("day"); //start of today 12 am
    const initialSchedule = {};
    initialSchedule[today.format("YYYY-DD-MM")] = true;
    const schedule = !appointments.length
      ? initialSchedule
      : appointments.reduce((currentSchedule, appointment) => {
          const { slot_date, slot_time } = appointment;
          const dateString = moment(slot_date, "YYYY-DD-MM").format(
            "YYYY-DD-MM"
          );
>>          !currentSchedule[slot_date]
            ? (currentSchedule[dateString] = Array(8).fill(false))
            : null;
>>          Array.isArray(currentSchedule[dateString])
            ? (currentSchedule[dateString][slot_time] = true)
            : null;
          return currentSchedule;
        }, initialSchedule);
    for (let day in schedule) {
      let slots = schedule[day];
>>      slots.length
        ? slots.every(slot => slot === true) ? (schedule[day] = true) : null
        : null;
    }
    this.setState({
      schedule: schedule
    });
  }

【问题讨论】:

标签: reactjs database mongodb compiler-errors


【解决方案1】:

在所有三种情况下,您都在做类似的事情

condition ? something = somethingElse : null

因此,如果条件是虚假的,则计算结果为 false null,这实际上没有任何意义。我认为如果您执行类似的操作会更容易阅读并消除警告

if (condition) something = somethingElse

第一种情况

!currentSchedule[slot_date]
  ? (currentSchedule[dateString] = Array(8).fill(false))
  : null;

应该是

if (!currentSchedule[slot_date]) {
  (currentSchedule[dateString] = Array(8).fill(false));
}

【讨论】:

    猜你喜欢
    • 2019-09-29
    • 2021-11-17
    • 2021-08-23
    • 1970-01-01
    • 2019-08-15
    • 2020-12-10
    • 2020-04-21
    • 2019-11-23
    • 1970-01-01
    相关资源
    最近更新 更多