When I write a sample of data from an array which, for example, is empty, then an error occurs. TypeError: Cannot read property text.

${data[0].text} 

Is it possible to somehow make it so that an empty string is returned, otherwise it is not very convenient to write if else constructs if there is a lot of nested data.

I use the vm module - the runInContext method.

  • only check. - Grundy
  • one
    you can stir up the wrapper: const p = (el, key, def = '') => (el && el[key] || def); . And in the code to write: ${p(data[0], 'text')} . - exvayn
  • $ {data [0]? data [0] .text? data [0] .text: '': ''} - Denis Podolyachenko

1 answer 1

Conditional statement :

 ${data[0]?data[0].text:""}