-3

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>
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
sky
  • 55
  • 1
  • 10
  • ... more info is needed. where is `this.getView()....`? In the initialization method? The issue here is `getModel` is not returning anything, so for some reason, your view has no model associated with it. – Thomas Matecki Dec 22 '16 at 16:48
  • @ThomasMatecki this.getView() is in the Initialization method of controller. I know it is not returning anything because it is throwing error at read function. I think something is wrong with parameters of read function but i am unable to figure it out. – sky Dec 23 '16 at 04:54
  • Does this answer your question? [How to get model on onInit?](https://stackoverflow.com/questions/48380000/how-to-get-model-on-oninit) – Boghyon Hoffmann Feb 03 '21 at 10:48

1 Answers1

0

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).

Marc
  • 6,051
  • 5
  • 26
  • 56
  • 1
    I have added onBeforeRendering function now i am able to see the data in oData variable through the alert. But i am not able to bind it to a model. I have tried using this.getView().setModel() and sap.ui.getCore().setModel() but not getting any data inside the model. In debugger console the execution stops at these two lines sap.ui.getCore().setModel()/this.getView().setModel(). – sky Dec 26 '16 at 06:03
  • 1
    The code is working now, i was trying the access this.getView().setModel() inside oModel.read() function which was incorrect. So i moved the this.getView.setModel() outside the read() function and now it's working. oData connection is established and model is set. – sky Dec 28 '16 at 07:28