Uncaught SyntaxError: Unexpected token '{'

Hello, I really need help. Iʼm working on a project on Cloudflare Workers, Iʼve been using Workers for a long time, but this is the first time Iʼve run into a serious problem. When I was going to save and deploy my Workers a warning popped up “Uncaught SyntaxError: Unexpected token ‘{’ at worker.js:107:248 (Code: 10021)” , and it prevented me from saving it. This is the line of code where my error is:

function setClass(selectedClass){const classButtons = document.querySelectorAll('.class'); classButtons.forEach((button) => {if (button.classList.contains('active')) {button.classList.remove('active');}}) let activeClass = document.querySelector(`.class-${selectedClass}`); activeClass.classList.add('active');}

at

document.querySelector(`.class-${selectedClass}`)

Curly brackets or braces ({) or grave accent or backtick (`), I donʼt know. I donʼt understand, because on my website it works very well. Please help me, itʼs been 3 days I canʼt solve the problem.

Hi @andybennison

There is nothing inherently wrong with that line. I suggest it possibly isn’t actually that line that is the issue, but another line. Without seeing the whole worker, I couldn’t say for sure though.

That looks like client-side Javascript. Workers don’t run in a browser and don’t have a DOM or document object.

Yeah, this doesn’t look like Worker code, either way, the issue is a missing semi-colon before the let activeClass.

If this is all one line then JS needs semi-colons to properly parse the tokens. Any editor should flag this up very quick.

1 Like