fix(discover): Use optional chaining on possibly undefined vars (#44418)
These variables sometimes can be `undefined`, so use optional chaining
to prevent an error from being thrown in these cases.
Fixes
[JAVASCRIPT-2E18](https://sentry.sentry.io/issues/3929508818/?project=11276&referrer=slack)
and
[JAVASCRIPT-2E0R](https://sentry.sentry.io/issues/3929115362/events/f8c5cc260a3a4c6da3cb7d8e19481870/?project=11276&referrer=previous-event)