0

How to check (eval?) if a given string/variable is valid

If I try

getData('/img/webshop/'+product.images[551].slug+'/'+product.images[1].src')

I get 'Cannot read property slug of undefined' if array item [551] does not exist

My getData function contains a Try catch function to check if [551] exists but the error is allready generated when the getData function is called

I could solve this by adding back-ticks

getData(`'/img/webshop/'+product.images[551].slug+'/'+product.images[1].src'`)

and sending this like a string but then I need to convert this string in getData, (eval?) but this doesn't work. ( remains string)

8
  • Does this answer your question? How to check if array element exists or not in javascript? Commented Feb 14, 2020 at 10:44
  • Short answer, check it before calling the method Commented Feb 14, 2020 at 10:45
  • Why would changing it to backticks solve the issue? Commented Feb 14, 2020 at 10:45
  • 1
    Sending a variable name as parameter won't send its content. Commented Feb 14, 2020 at 10:46
  • @evolutionxbox I want to try catch in a seperate function, after calling the function. back-ticks make sending without error. I was a try.. Commented Feb 14, 2020 at 10:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.