[SalesForce] LWC: Uncaught (in promise) TypeError: Cannot read property ‘recordTypeInfos’ of undefined

This error has recently appeared for me again on an LWC, but only the very first time I open a page for this record type after logging into the org – thereafter never again for my session. And I can't replicate this in my Dev sandbox at all – only in my production org and QA (which is a full sandbox).
The only time I get it again is if I log out of the org and back in again. It is extremely frustrating as I don't understand what the problem is.

I would appreciate any help in sorting this out.

enter image description here

My HTML:

<div class="slds-card__body slds-card__body_inner">
<div class="slds-var-m-around_medium slds-text-heading_small">
  <template if:true={reasonPicklistValues.data}>
    <lightning-combobox
      name="reason"
      label="Select a reason"
      value={value}
      placeholder="-Select-"
      options={reasonPicklistValues.data.values}
      onchange={handleChange}
      class="slds-var-m-around_x-small">
    </lightning-combobox>
  </template>
</div>
</div>

And my JS:

  @api recordId;
  @api objectApiName;

  @track objectInfo;

  @wire(getObjectInfo, { objectApiName: CERTIFICATE_OBJECT })
  objectInfo;
  get recordTypeId() {
    if (this.objectInfo.data) {
      if (this.objectInfo.data.recordTypeInfos) {
        const rtis = this.objectInfo.data.recordTypeInfos;
        return Object.keys(rtis).find(
          (rti) => rtis[rti].name === "XYZ Certificate"
        );
      }
    }
    return null;
  }

  @wire(getPicklistValues, {
    recordTypeId: "$recordTypeId",
    fieldApiName: GENERATEMANUALREASON_FIELD
  })
  reasonPicklistValues;

Best Answer

I've reproduced your code in a simplified snippet and it does work for me. I think your error may be happening because what @Nick Cook said in a comment - As @wire tracks the property already, so you don't need to @track it. Here you have the simplified code for reference:

import { LightningElement, wire, api } from "lwc";
import { getObjectInfo } from "lightning/uiObjectInfoApi";
import ACCOUNT_OBJECT from "@salesforce/schema/Account";

export default class RecordTypesError extends LightningElement {
  @api recordId;
  @api objectApiName;

  @wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
  objectInfo;

  get recordTypeId() {
    if (this.objectInfo.data) {
      if (this.objectInfo.data.recordTypeInfos) {
        const rtis = this.objectInfo.data.recordTypeInfos;
        return Object.keys(rtis).find((rti) => rtis[rti].name === "test");
      }
    }
    return null;
  }