Robison William
Robison William

Reputation: 399

WebStorm error: expression statement is not assignment or call

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.

Upvotes: 39

Views: 66689

Answers (3)

DaveM
DaveM

Reputation: 81

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.

Upvotes: 8

Sebastian Hurtado
Sebastian Hurtado

Reputation: 1689

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.

Upvotes: 26

gauravmuk
gauravmuk

Reputation: 1616

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.

Upvotes: 51

Related Questions