Search code examples
odatasapui5

Odata connets but cant get the data


I'm trying to develop an sapui app that connets to tha sap and collect some data, here is my code, Extracted request part.

var sServiceUrl = "http://isttst5007.unilever.com:8000/sap/opu/odata/sap/ZSNAP_BEG_SRV";
    var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true);

    var oJsonModel = new sap.ui.model.json.JSONModel();

    oModel.read("/DumpsetSet?", null, null, true, function (oData, repsonse) {
        oJsonModel.setData(oData);
    });
    sap.ui.getCore().setModel(oJsonModel)

    console.log(oJsonModel.getProperty("/d/results"));

It connects server and asks username, password. But however It can not collect data.


Solution

  • The fact that the read is a asynchronous task could lead to the problem that when you do the console.log, the model isn't filled with the data yet. Could you try to do the console.log inside the success function of the read function? If there is data displayed, the asynchronous task is causing the "problem".

    var sServiceUrl = "../sap/opu/odata/sap/ZSNAP_BEG_SRV";
    var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true);
    
    var oJsonModel = new sap.ui.model.json.JSONModel();
    
    oModel.read("/DumpsetSet?", null, null, true, function (oData, repsonse) {
        oJsonModel.setData(oData);
        // Here the JSON Model is filled with the right data
        sap.ui.getCore().setModel(oJsonModel)
        console.log(oJsonModel.getProperty("/d/results"));
    });