Javascript WebStorm 错误:表达式语句不是赋值或调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44618100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:39:36  来源:igfitidea点击:

WebStorm error: expression statement is not assignment or call

javascriptnode.jswebstorm

提问by Robison William

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

我正在使用 WebStorm,但遇到一个我无法理解的错误。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.

PS:代码工作正常。

回答by gauravmuk

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

您需要将 JavaScript 语言版本更改为 ES6。更改此设置应该可以解决问题:

Settings to change Javscript version to ES6

将 JavaScript 版本更改为 ES6 的设置

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

在某些情况下,您可能需要重新启动 IDE 才能正确反映更改。

回答by Juan Hurtado

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

问题在于,如果该语句在函数内执行以下任何操作,WebStorm 将显示警告:

  • 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 viewsthat function as unnecessary and tries to help you catch unused code.

换句话说,WebStorm 认为这些功能是不必要的,并试图帮助您捕获未使用的代码。

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 notto change WebStorm settings.

答案是不要更改 WebStorm 设置。