How to get model on onInit?

You can avoid setTimeout, as mentioned in this answer, by using the v2.ODataModel API metadataLoaded instead which returns a promise. The promise is fulfilled once the service metadata is loaded successfully.

onInit: async function() {
  const oPayerModel = this.getOwnerComponent().getModel("SalesInvoices");
  try {
    await oPayerModel.metadataLoaded(true);
    const oServiceMetadata = oPayerModel.getServiceMetadata(); // NOT .getMetadata()
    // ...
  } catch (oError) {/* ... */}
},

About the model being undefined in onInit, here are answers with better explanations:

Leave a Comment