[英]Getting linter error “Unexpected token =” with destructuring assignment on previously declared variables
let data = []
let count, meta
if (req.params.id === 'count') {
// get count only
} else {
{ data, count } = await myModel.find(filter, options)
meta = { count }
}
gives me a linter error: "Parsing error: Unexpected token =" at the destructuring assignment.给我一个 linter 错误:“解析错误:解构赋值时出现意外标记 =”。 Is this just a setting in the linter config I need to change?
这只是我需要更改的 linter 配置中的一个设置吗?
I can get rid of the error using parenthesis, but why is this required?我可以使用括号消除错误,但为什么需要这样做?
({ data, count } = await myModel.find(filter, options))
Any statement that begins with {... }
will always be have the bracketed sequence parsed as a code block rather than as an object (including a destructuring object expression).任何以
{... }
开头的语句将始终将括号中的序列解析为代码块,而不是 object(包括解构 object 表达式)。
MDN clarifies this exact case in its documentation on destructuring : MDN 在其关于解构的文档中阐明了这个确切的情况:
var a, b; ({a, b} = {a: 1, b: 2});
Notes: The parentheses
(... )
around the assignment statement are required when using object literal destructuring assignment without a declaration.注意:使用没有声明的 object 文字解构赋值时,赋值语句周围的括号
(... )
是必需的。
{a, b} = {a: 1, b: 2}
is not valid stand-alone syntax, as the{a, b}
on the left-hand side is considered a block and not an object literal.{a, b} = {a: 1, b: 2}
不是有效的独立语法,因为左侧的{a, b}
被认为是一个块,而不是 object 文字。However,
({a, b} = {a: 1, b: 2})
is valid, as isvar {a, b} = {a: 1, b: 2}
但是,
({a, b} = {a: 1, b: 2})
是有效的,var {a, b} = {a: 1, b: 2}
Your
(... )
expression needs to be preceded by a semicolon or it may be used to execute a function on the previous line.您的
(... )
表达式需要以分号开头,或者它可用于在上一行执行 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.