Search code examples
sapui5

Not able to connect to OData services


I am trying to connect to backend ABAP system through OData services but I am getting error as shown in the screenshot.

library-preload.js:522 Uncaught TypeError: Cannot read property 'read' of undefined(…)

Error

Here's the code of various files:

neo-app.json:

{
  "path": "/DB7",
  "target": {
    "type": "destination",
    "name": "DB7"
  },
  "description": "DB7"
}

Component.js

 init: function() {  var oModel = new    sap.ui.model.odata.v2.ODataModel(this.getMetadata().getConfig().serviceUrl);
   this.setModel(oModel,"data");

}

manifest.json

{
"_version": "1.1.0",
"sap.app": {
    "_version": "1.1.0",
    "id": "com.abc.cup",
    "type": "application",
    "i18n": "i18n/i18n.properties",
    "applicationVersion": {
        "version": "1.0.0"
    },
    "title": "{{appTitle}}",
    "description": "{{appDescription}}",
    "sourceTemplate": {
        "id": "ui5template.basicSAPUI5ApplicationProject",
        "version": "1.32.0"
    },
    "dataSources": {
        "invoiceRemote": {
        "uri": "/destinations/DB7/sap/opu/odata/ATSH/UI5_DISPLAY_SRV/",
        "type": "OData",
        "settings": {
            "odataVersion": "2.0"
    }
  }
}
},
"sap.ui": {
    "_version": "1.1.0",
    "technology": "UI5",
    "icons": {
        "icon": "",
        "favIcon": "favicon.ico",
        "phone": "",
        "phone@2": "",
        "tablet": "",
        "tablet@2": ""
    },
    "deviceTypes": {
        "desktop": true,
        "tablet": true,
        "phone": true
    },
    "supportedThemes": ["sap_hcb", "sap_bluecrystal"]
},
"sap.ui5": {
    "_version": "1.1.0",
    "rootView": {
        "viewName": "com.abc.cop.view.View1",
        "type": "XML"
    },
    "dependencies": {
        "minUI5Version": "1.30.0",
        "libs": {
            "sap.ui.core": {},
            "sap.m": {},
            "sap.ui.layout": {}
        }
    },
    "contentDensities": {
        "compact": true,
        "cozy": true
    },
    "models": {
        "":{
            "dataSource": "invoiceRemote",
            "settings":{}
        },
        "i18n": {
            "type": "sap.ui.model.resource.ResourceModel",
            "settings": {
                "bundleName": "com.abc.cop.i18n.i18n"
            }
        }
    },
    "resources": {
        "css": [{
            "uri": "css/style.css"
        }]
    },
    "routing": {
        "config": {
            "routerClass": "sap.m.routing.Router",
            "viewPath": "com.abc.cop.view",
            "controlId": "appId",
            "controlAggregation": "pages",
            "transition": "fade"
        },
        "routes": [{
            "name": "main",
            "pattern": "",
            "target": ["initialScreen"]
        }, {
            "name": "moreDetails",
            "pattern": "moreDetails",
            "target": ["moreDetails"],
            "greedy": true
        }
        ],
        "targets": {
            "main": {
                "viewType": "XML",
                "transition": "slide",
                "viewName": "View1",
                "viewId": "view1",
                "viewLevel": 1
            },
            "moreDetails": {
                "viewType": "XML",
                "transition": "fade",
                "viewName": "View2",
                "viewId": "view2",
                "clearAggregation": true,
                "viewLevel": 2,
                "controlId": "appId"
            },
            "initialScreen": {
                "viewType": "XML",
                "transition": "fade",
                "clearAggregation": true,
                "viewName": "View3",
                "viewId": "view3"
            }
        }
    }
}

}

Controller.js

onBeforeRendering: function() {
        var oModell = new sap.ui.model.odata.ODataModel(
            "https://webidetesting3386376-p1942296689trial.dispatcher.hanatrial.ondemand.com/destinations/DB7/sap/opu/odata/ATSH/UI5_DISPLAY_SRV/"
        );

        //var oJSONModel = new sap.ui.model.json.JSONModel();
        oModell.read("SY_INFOSet(Customer='ABC',Sysid='DB7')", null, null, true, function(oData, oResponse) {
            alert("Read successful: " + JSON.stringify(oData));

            this.getView().setModel(oModell, "jsonData");

            //var oModel22 = new sap.ui.model.json.JSONModel(oData);
            //sap.ui.getCore().setModel(oModel22, "jsonData");

        }, function() {

            alert("Read failed");
        });
    },

View.xml

<ObjectHeader class="HeaderObjectwidth" id="objectHeader" title="{i18n>systemId} - {SYS_INFOSet/Sysid}" number="{i18n>numUsers} {SYS_INFO/Customer}" numberUnit="{i18n>versionId} {SYS_INFO/Sysid}">
   <attributes>
       <ObjectAttribute title="{i18n>custName}" text="{jsonData>/FILE_UPD_UI5Set/Customer}"/>
       <ObjectAttribute title="{i18n>osDetails}" text="{jsonData>Customer}"/>
       <ObjectAttribute title="{i18n>dbDetails}" text="{jsonData>/Value}"/>
   </attributes>
</ObjectHeader>

Solution

  • The error states that the method read cannot be called because the object that it's called on is undefined. This means that this.getView().getModel() returns undefined.

    As far as I know the models of a view are NOT available during the onInit lifecycle hook. You have to choose a different lifecycle method.

    A list of all lifecycle hooks can be found in the API description of the controller class.

    I would suggest using onBeforeRendering since it's called directly after onInit.

    See also this github issue which describes the same problem (and provides some answers as well).