2016-12-22 79 views
-2

我想通過OData服務連接到後端ABAP系統,但出現錯誤,如截圖所示。無法連接到OData服務

庫preload.js:522遺漏的類型錯誤:

新的應用程序:無法讀取屬性未定義(...)

Error

這裏的各種文件中的代碼 '讀'以.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": "", 
     "[email protected]": "", 
     "tablet": "", 
     "[email protected]": "" 
    }, 
    "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> 
+0

你可以從manifest中粘貼'sap.ui5'命名空間的模型嗎? –

+0

...需要更多信息。 'this.getView()....'在哪裏?在初始化方法?這裏的問題是'getModel'沒有返回任何東西,所以出於某種原因,你的視圖沒有與它關聯的模型。 –

+0

@RahulBhardwaj我已經從manifest.json中添加模型 – sky

回答

0

錯誤指出方法read不能被調用,因爲它被調用的對象是undefined。這意味着this.getView().getModel()返回undefined

就我所知,在onInit生命週期掛鉤期間,視圖模型不可用。您必須選擇不同的生命週期方法。

所有生命週期掛鉤的列表可以在API description of the controller class中找到。

我建議使用onBeforeRendering,因爲它是在onInit之後直接調用的。

另請參閱this github issue其中描述了同樣的問題(並提供了一些答案)。

+0

我已經添加了onBeforeRendering函數,現在我能夠通過警報查看oData變量中的數據。但我無法將其綁定到模型。我試過使用this.getView().setModel()和sap.ui.getCore()。setModel()但沒有得到模型內的任何數據。 在調試器控制檯中,執行停止在這兩行sap.ui.getCore()。setModel()/ this.getView()。setModel()。 – sky

+0

代碼現在正在工作,我試圖訪問this.getView().setModel()裏面的oModel.read()函數是不正確的。所以我將read.setModel()移到了read()函數之外,現在它正在工作。 o數據連接建立並設置模型。 – sky