admin管理员组

文章数量:1402803

I try to evaluate this expression in Node.js v8.9.4:

`${ xxx 123 }`

It throws the error

Missing } in template expression

I understand that the expression above should cause an error because the contents inside ${} are not valid JavaScript. But why does the error-message claim a } is missing? I can clearly see an opening { and a closing }. Why is it saying a } is missing?

I try to evaluate this expression in Node.js v8.9.4:

`${ xxx 123 }`

It throws the error

Missing } in template expression

I understand that the expression above should cause an error because the contents inside ${} are not valid JavaScript. But why does the error-message claim a } is missing? I can clearly see an opening { and a closing }. Why is it saying a } is missing?

Share Improve this question edited Nov 20, 2024 at 16:39 dumbass 27.3k4 gold badges38 silver badges74 bronze badges asked Mar 12, 2018 at 3:01 Panu LogicPanu Logic 2,2711 gold badge19 silver badges24 bronze badges 5
  • 3 xxx 123 is not a valid expression – Jorg Commented Mar 12, 2018 at 3:03
  • 1 Why is it saying a '}' is missing? because, when trying to figure out what you're trying to do, I guess that's the parser error that es up – Jaromanda X Commented Mar 12, 2018 at 3:03
  • 1 I think because it expects ${ xxx }? – rafaelgomesxyz Commented Mar 12, 2018 at 3:04
  • Because it is expecting the the interpolation to be closed between expressions; xxx and 123 – treyhakanson Commented Mar 12, 2018 at 3:07
  • 1 Yeah, Jorg is correct, I think the problem is that xxx 123 is not a valid expression, so however it is trying to parse it this is the best error it can e up with for whatever it things you are trying to do. Are you perhaps intending it to be `${xxx} 123` where xxx is some variable you have saved? – Alexander Nied Commented Mar 12, 2018 at 3:07
Add a ment  | 

1 Answer 1

Reset to default 6

You say there is a closing brace, but the JS parser doesn't get that far because it breaks between xxx and 123.

The syntax expects an opening brace, an expression, then a closing brace. What it gets is an opening brace, an expression then another expression. So at that point the error is precisely what is says. And in fact, if you add the brace as it suggests, it will work ${ xxx } 123 }. It may be not what you intended, but you can't say it is ill-formed.

本文标签: