Home > Blockchain >  PhpStorm/JavaScript Expression statement is not assignment or call
PhpStorm/JavaScript Expression statement is not assignment or call

Time:08-31

I'm trying to make a method the cleanest I can.

Depending on what a integer number is, I'm creating a different date format, so I end up having this:

getRanges() {
            var currentDate = new Date();
            //Esto seguro que se puede simplificar
            const format = (format) => {
                this.formatedTimeRange.start = moment(new Date(currentDate.getTime() - this.timeRange * 60000)
                    , "YYYY-MM-DDTHH:mm:ss.SSSZ").format(format);
                this.formatedTimeRange.end = moment(new Date(this.linesData[0].x[this.linesData[0].x.length-1])
                    , "YYYY-MM-DDTHH:mm:ss.SSSZ").format(format);
            }

            const Ranges = {
                '1': format("YYYY-MM-DD HH:mm:ss"),
                '5': format("YYYY-MM-DD HH:mm:ss"),
                '15': format("YYYY-MM-DD HH:mm:ss"),
                '60': format("YYYY-MM-DD HH:mm"),
                '180': format("YYYY-MM-DD HH:mm"),
                '360': format("YYYY-MM-DD HH:mm"),
                '720': format("YYYY-MM-DD HH:mm"),
                '1440': format("YYYY-MM-DD HH:mm"),
                '2880': format("YYYY-MM-DD HH"),
                '10080': format("YYYY-MM-DD HH"),
                '43200': format("YYYY-MM-DD"),
            }

            Ranges[this.timeRange];
        }

But I get this warning message:

Expression statement is not assignment or call in line Ranges[this.timeRange];

P.S. The code works fine.

CodePudding user response:

If you want to return this value, you have to add the expression "return" or if you want to print the Range[this.timeRange]; you should add the "print" expression. Alternatively you can just take this line out, because as the comment above i dont really see the purpose of this line.

CodePudding user response:

You're formatting your data with every possible format, every time, but it seems you wanted to pick one specific format according to this.timeRange value. The following change will make it work like that:

const Ranges = {
  '1': () => format("YYYY-MM-DD HH:mm:ss"),
  '5': () => format("YYYY-MM-DD HH:mm:ss"),
  // etc.
};

if (Ranges[this.timeRange]) {
  Ranges[this.timeRange]();
}

So, Ranges becomes a set of formatters indexed by some ID. In the last if statement specific formatter is chosen and then executed (if it exists).

  • Related