0

I created a mockserver for testing purpose on my sapui5 app.

The definition of the dataSources structure in the manifest.json file looks as following:

"dataSources": {
    "NorthwindService": {
        "uri": "/",
        "type": "OData",
        "settings": {
            "odataVersion": "2.0",
            "localUri": "localService/metadata.xml"
        }
    }
}

and the model definition:

"models": {
    "i18n": {
        "type": "sap.ui.model.resource.ResourceModel",
        "settings": {
            "bundleName": "ch.app.northwind.i18n.i18n"
        }
    },
    "northwind": {
        "uri": "",
        "type": "sap.ui.model.odata.v2.ODataModel",
        "settings": {
            "defaultOperationMode": "Server",
            "defaultBindingMode": "OneWay",
            "defaultCountMode": "Request"
        },
        "dataSource": "NorthwindService",
        "preload": true
    }
},

calling the model controller:

this.getView().getModel("northwind")
.read("/Customers/$count", {
  success: function(oData) {
    jQuery.sap.log.info(oData);
  },
  error: function(oError) {
    jQuery.sap.log.info(oError);
  }
});   

It complains:

Overview.controller.js?eval:29 Uncaught (in promise) TypeError: Cannot read property 'read' of undefined
    at f.queryTotalCustomers (Overview.controller.js?eval:29)
    at f.onInit (Overview.controller.js?eval:12)
    at f.a.fireEvent (EventProvider-dbg.js:228)
    at f.a.fireEvent (Element-dbg.js:431)
    at f.fireAfterInit (ManagedObjectMetadata-dbg.js:568)
    at r (Component-dbg.js:162)
    at f.h.runAsOwner (Component-dbg.js:549)
    at P (View-dbg.js:429)
    at eval (View-dbg.js:467)  

What am I doing wrong?

softshipper
  • 32,463
  • 51
  • 192
  • 400

1 Answers1

0

First of all: do not set '/' or empty URI for your data source and model. Give them a path you like, different than that. '/' is reserved for the main page, or the index.html

Second, make sure get your model once it is instantiated and no before. Probably you are getting the model in the onInit event, when it is not ready yet. Use another event or set an event handler for the 'metadataLoaded' event of your model.

Rafael López Martínez
  • 2,225
  • 2
  • 15
  • 23