简体   繁体   English

获取 linter 错误“Unexpected token =”,对先前声明的变量进行解构赋值

[英]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 is var {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.

相关问题 解构赋值导致错误:“unexpected token =” - Destructuring assignment cause error: “unexpected token =” 收到“意外令牌错误” - Getting an “Unexpected Token Error” 获取错误 - >意外的令牌} - Getting error -> Unexpected token } 如何将 javascript object 解构分配给 ZA2F2ED4F8EBC2CBB14C21A2DDC 的 scope 中已声明的变量 - How to perform a destructuring assignment of a javascript object to already declared variables inside the scope of a class 将分配变量解构为全局常量 - Destructuring Assignment Variables as Global Constants 在 Concat 函数中出现错误:“Uncaught SyntaxError: Invalid destructuring assignment target” - Getting error: "Uncaught SyntaxError: Invalid destructuring assignment target" in Concat function 当我使用解构赋值来简化我的道具时出错 - getting error when I use destructuring assignment to simplify my props 在Create-React-App中键入具有解构分配参数的函数时出现意外令牌 - Unexpected token when typing a function with destructuring assignment parameters in Create-React-App 获取javascript意外令牌错误 - Getting javascript unexpected token error 在 Javascript 中获取“意外令牌”错误 - Getting "unexpected token" error in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM