-2

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>
    </div>
    </div>
    </div>
    </div>
    )}
    </div>`

Any help would be appreciated!

user16732475
  • 15
  • 1
  • 6

2 Answers2

1

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>
</div>
</div>
</div>`
)}

</div>`;
mstephen19
  • 1,733
  • 1
  • 5
  • 20
1

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')}
            </div>
          </div>
        </div>
      </div>
    `).join('\n')}
  </div>
</div>
`

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')}
        </div>
      </div>
    </div>
  </div>
`

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')}
  </div>
</div>
`

All untested, but roughly correct.

Step 4: Give things meaningful names.

  • object => products
  • el => product
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    This was very useful, thank you. Accept that my code is quite messy and difficult to read so will try it keep the overall component as wee as possible. Still in my infancy as a developer so every day is a school day! – user16732475 May 12 '22 at 15:57
  • @user16732475 It's definitely a process, both fun and frustrating :thumbs-up: – Dave Newton May 12 '22 at 15:57