35

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.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
Robison William
  • 359
  • 1
  • 3
  • 5

2 Answers2

50

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.

gauravmuk
  • 1,606
  • 14
  • 20
  • 2
    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 project – amitgur Oct 12 '17 at 07:53
  • Considering the current approval state of ES6, can this be taken as a general recommendation for JSX based projects? – gnzg Feb 27 '18 at 12:20
  • 46
    This answer is __NOT__ correct as I have my settings set to Ecmascript 6, but I still get the warning. – Jim May 07 '18 at 15:16
  • @Jim I saw the same thing and restarting the IDE fixed the issue for me. – Matthew May 30 '18 at 20:17
  • 1
    I did not even restart whole IDE. Only changed option 'Only type-based..' ON and OFF and it fixed problem. – JerzySBG Jul 04 '18 at 11:49
  • 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 :-) – Zaphoid Oct 18 '18 at 11:47
  • What if I want React JSX, which I do? – html_programmer May 24 '19 at 20:33
  • 2
    I have 6+ and still get that warning. In `this.currentExercise = this.availableExercises.find( (exercise: Exercise) => {exercise.id === exerciseId});` – Mawg says reinstate Monica Aug 13 '21 at 08:56
22

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.

Juan Hurtado
  • 1,540
  • 1
  • 8
  • 9