on 12-17-2014 6:36 AM
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
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);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.