WebStorm error: expression statement is not assignment or call
Asked Answered
N

3

39

I'm using WebStorm and I'm getting an error that I can't understand. Node.js + MongoDB.

var mongoose = require('mongoose');

mongoose.Promise = global.Promise;
mongoose.connect(' mongodb://localhost:27017/TodoApp');

var Todo = mongoose.model('Todo', {
    text: {
        type: String
    },
    completed: {
        type: Boolean
    },
    completedAt: {
        type: Number
    }
});

var newTodo = new Todo({
    text: 'Cook dinner'
});

The problem is in this block:

newTodo.save().then((doc) => {
    console.log('Saved todo', doc);
}, (e) => {
    console.log('Unable to save todo')
})

P.S.: The code works fine.

Nahshun answered 18/6, 2017 at 18:17 Comment(2)
console.log('Unable to save todo') doesn't have a semicolon ; ?Quinquennium
semicolon will give just warning so it does not matter @YashKaranke. Many coding styles does even follow no semicolon rule.Ember
S
51

You need to change JavaScript Language Version to ES6. Changing this setting should fix the issue:

Settings to change Javscript version to ES6

In some scenarios, you might need to restart your IDE for the changes to reflect properly.

Sentry answered 18/6, 2017 at 18:48 Comment(8)
gauravmuk answer is correct, pay attention that you can change the Javascript from the Webstorm->Preferences menu and not from the File->Default Settings. The second one has no effect on your current projectLowlife
Considering the current approval state of ES6, can this be taken as a general recommendation for JSX based projects?Pauwles
This answer is NOT correct as I have my settings set to Ecmascript 6, but I still get the warning.Batik
@Batik I saw the same thing and restarting the IDE fixed the issue for me.Brittaney
I did not even restart whole IDE. Only changed option 'Only type-based..' ON and OFF and it fixed problem.Heti
Thanks a lot! This also works for IntelliJ IDEA (2018). But the preferences are called "settings": File -> Settings... (Ctrl+Alt+S). I therefore added the Intellij Tag :-)Triggerfish
What if I want React JSX, which I do?Superphosphate
I have 6+ and still get that warning. In this.currentExercise = this.availableExercises.find( (exercise: Exercise) => {exercise.id === exerciseId});Shoshonean
P
25

The problem is that WebStorm will show a warning if that statement isn't doing any of the following within a function:

  • Calling another function
  • Making any sort of assignment
  • Returning a value
  • (There may be more, but those are the ones I know of)

In other words, WebStorm views that function as unnecessary and tries to help you catch unused code.

For example this will show the warning:

const arr = [1, 2];
const willShowWarning = arr.map(num => {
    num + 1;
});

Adding a return will take the warning away:

const arr = [1, 2];
const willNotShowWarning = arr.map(num => {
    return num + 1;
});

The answer is not to change WebStorm settings.

Paquito answered 6/8, 2018 at 4:38 Comment(0)
O
6

I've found that sometimes the IDE state will get confused if I've been editing in that area and highlight the code, even though the code is correct. In those cases deleting and re-pasting the code block fixes the issue for me.

Orvas answered 30/4, 2024 at 13:41 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.