Question

I am consuming ODATA service to get the resulting dataset from a table. I get the data in JSON format like as shown below.

$.getJSON("../TEST_ODATA3.xsodata/COMPAREDATA?$format=json&$select=WERT&$filter=LIFNR eq '"+supplier+"' and ARTIKEL eq '"+artikel+"' and STOREID eq '"+storeId+"' and BUSINESS_DATE eq datetime'"+date_time+"'",
                                    function(response){
                                for (i = 0; i < response.d.results.length; i++) {
                                    console.log(response.WERT);}});

Everything works fine. But when I try to access the column name WERT from the response, I get the value as undefined in the browser console. But when I copy the same link including host and port on the browser I see the column name and the value in the response. Can anyone tell me what I am missing in the above code. I also tried console.log(response.d.results.WERT);

UPDATE : The response looks like this:

{
d: {
results: [
{
__metadata: {
uri: "host:port/TEST_ODATA3.xsodata/COMPAREDATA('1')",
type: "TEST_ODATA3.COMPAREDATAType"
},
WERT: "35.26"
}
]
}
}
Was it helpful?

Solution

results is an array according to your json . You can access it like this

  response.d.results[0].WERT

OTHER TIPS

A clean indentation would have show you the problem. Your object WERT is in d.results array, you must access it by response.d.results[0].WERT

For loops I prefer to use jQuery each:

$.getJSON("../TEST_ODATA3.xsodata/COMPAREDATA?$format=json&$select=WERT"+
    "&$filter=LIFNR eq '"+supplier+
    "' and ARTIKEL eq '"+artikel+
    "' and STOREID eq '"+storeId+
    "' and BUSINESS_DATE eq datetime'"+date_time+"'",
    function(response){
        $.sap.each(response.d.results, function(i, oItem){
            console.log(oItem.WERT);
        });
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top