skip to Main Content

this is the code

<div className="flex-box d-flex justify-content-between align-items-center">
    <h6>Price</h6>
    <span>
        $ {product.price.toLocaleString('en-US', {maximumFractionDigits: 2,})}
    </span>
</div>

it shows no error

image

but when I look at it, it shows this

enter image description here

how do I solve this? I just want to price to be like $ 1,399.99

2

Answers


  1. According to the error message, on the run time, you don’t have the product.price so it results in "undefined".
    you have to check for its availability before using any other methods chained to it.
    you can simply do this:

    product?.price?.toLocaleString('en-US', {maximumFractionDigits: 2,})
    

    By adding ? you are using optional chaining. and you won’t see the error.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search