0

I get TypeError: Cannot read properties of undefined (reading 'latestDocDate') with this:

if (this.state.latestDocDate === undefined || this.props.docRow[0].docDate > this.state.latestDocDate) {

There is a possibility this.state.latestDocDate could be undefined but I thought this.state.latestDocDate === undefined might cater for that. How do I resolve this?

runnerpaul
  • 5,942
  • 8
  • 49
  • 118
  • 2
    `this.state` is undefined, evaluating the left-hand side fails _before_ comparison. Check for that first. Read up on optional chaining, if that's available in your runtime. – jonrsharpe Jul 12 '22 at 21:55
  • 1
    Does this answer your question? [Detecting an undefined object property](https://stackoverflow.com/questions/27509/detecting-an-undefined-object-property) – DjaouadNM Jul 12 '22 at 21:56
  • Also you can use null safe: `this.state?.latestDocDate` – LucianDex Jul 12 '22 at 22:20

0 Answers0