Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be
on-topic
here, this one was resolved in a way less likely to help future readers.
Closed
12 months ago
.
I keep getting the error message ',' expected.ts(1005) when trying to dynamically render a modal on Typescript. I'm almost certainly missing a closing curly bracket somewhere but cant figure out where.
`<div class="modalWrap">
<h2 class="modalHeader">Shop The Look</h2>
<div class="modalContent">${object.map((el) =>
`<div class="prodDetails">
<img class="prodImg" alt="prodImage" src = ${el.primaryImage.url}></img>
<p class="prodName">${el.name}</p>
<p class="prodPrice">${el.price.value}</p>
<div class = "selectWrapper">
<div class="selectNumber">
<div class="numberWrapper">
${el.sizeOptions
.map(
(option) => `
<p class="selectSize" >${option.value}</p>`
.join('')}
</div>`
Any help would be appreciated!
–
–
–
Less of an answer than a rant with good intentions.
This code is illegible, and because of that, finding the missing backtick is extremely tedious. Here's how to make it less awful. * Edited for brevity.
Step 1: Indent. We're not savages.
This step alone is enough to resolve the issue.
<div class="modalWrap">
<h2 class="modalHeader">Shop The Look</h2>
<div class="modalContent">
${object.map(el => `
<div class="prodDetails">
<img class="prodImg" alt="prodImage" src = ${el.primaryImage.url}></img>
<p class="prodName">${el.name}</p>
<p class="prodPrice">${el.price.value}</p>
<div class = "selectWrapper">
<div class="selectNumber">
<div class="numberWrapper">
${el.sizeOptions.map(option => `
<p class="selectSize" >${option.value}</p>
`).join('\n')}
`).join('\n')}
Step 2: Don't do so much at once. Let your code breathe.
There are two pseudo-"components" to pull out, both functions to map. This clarifies their intent and reduces the surface area of the mainline code.
The inner one is simple:
const makeOption = opt => `<p class="selectSize">${option.value}</p>`
The outer one is incrementally more complex, but by maintaining indentation, adding some clarifying whitespace, and using the inner "component", it's much cleaner:
const makeProdDetail = el => `
<div class="prodDetails">
<img class="prodImg" alt="prodImage" src=${el.primaryImage.url}></img>
<p class="prodName">${el.name}</p>
<p class="prodPrice">${el.price.value}</p>
<div class="selectWrapper">
<div class="selectNumber">
<div class="numberWrapper">
${el.sizeOptions.map(makeOption).join('\n')}
The smaller the functional chunks are the easier spotting and mitigating mistakes is. It's also easier to modify/update/parameterize/etc.
Step 3: Tie it all together
Now the overall component is wee:
<div class="modalWrap">
<h2 class="modalHeader">Shop The Look</h2>
<div class="modalContent">
${object.map(makeProdDetail).join('\n')}
All untested, but roughly correct.
Step 4: Give things meaningful names.
object => products
el => product
–
You didn't close the string literal that was being mapped from object:
const test = `<div class="modalWrap">
<h2 class="modalHeader">Shop The Look</h2>
<div class="modalContent">${object.map(
(el) =>
`<div class="prodDetails">
<img class="prodImg" alt="prodImage" src = ${el.primaryImage.url}></img>
<p class="prodName">${el.name}</p>
<p class="prodPrice">${el.price.value}</p>
<div class = "selectWrapper">
<div class="selectNumber">
<div class="numberWrapper">
${el.sizeOptions
.map(
(option) => `
<p class="selectSize" >${option.value}</p>`
.join('')}
</div>`
</div>`;