cancel
Showing results for 
Search instead for 
Did you mean: 

OData.request Simple GET, save response into a Variable

Former Member
0 Kudos

Hi Guys

This is my problem.  I am using a OData.request to get a value by introducing a specific data and I would like to save the response into a variable.

In this example I get the value and I can display it on the console log, but only inside the OData.request... If I run the the console log outside the OData request, the variable is undefined. How do I save the response of a OData Service in a variable.

onEmail: function (evt){

            this._onEmail(evt);

  },

  _onEmail: function (evt){

           var oGetInput = this.getView().byId("iInput");

           var oGetValue=oGetInput.getValue();

           var serviceRoot = "proxy/http/XXXX:8000/sap/opu/odata/sap/XXXX/Search?EMAIL='"+oGetValue+"'";

           var headers = { "Content-Type": "application/json", Accept: "application/json" };

           var request = {

                 requestUri: serviceRoot,

                 method: "GET",

                 headers: headers,

                 data: null

            };

  OData.request(

            request,

            function (data, response) {

                      var filtedPeople = data.results;

                      this.oPARTNER = filtedPeople[0].PARTNER;

                      console.log("this.oPartner"+this.oPARTNER);

          ////////////////////////////////////////////////////////////////////////////

          // the result of the console log is correct

          ////////////////////////////////////////////////////////////////////////////////////

  },

  function (err) {

  alert("Fail: " + err.Message);

  }

  );

  console.log("this.oPartner.outside"+this.oPARTNER);

          ////////////////////////////////////////////////////////////////////////////

          // In here the parameter is undefined

          ////////////////////////////////////////////////////////////////////////////////////

  },

Regards

Henry

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

This is how I solve it...

onLogin: function (evt){

....

var that = this;

OData.request(

          request,

          function (data, response) {

                        var filtedPeople = data.results;

                        that.oPARTNER = filtedPeople[0].PARTNER;

                        that._onLogin2();      

          },       

          function (err) {

                         alert("Fail: " + err.Message);

          },

)},

_onLogin2: function(evt){

         var oPARTNERID = this.oPARTNER;

         console.log("partner outside: "+oPARTNERID);

}

Answers (1)

Answers (1)

pfefferf
Active Contributor
0 Kudos

Hello Henry,

the problem is that "this is not this". "this" points to the object which owns the code (please have a look to e.g. JavaScript Function Invocation). So this points to two different objects in your example.

Before OData.request ... you can say "that = this;" and than you can use that within the response function of the OData request. Afterwards outside you can use that.oPARTNER to log the data in the console.

Regards,

Florian