cancel
Showing results for 
Search instead for 
Did you mean: 

Create a read request on odata service with promise

0 Kudos

Hello,

My code for the request is:

onInit: function () {
        var oModel = this.getOwnerComponent().getModel();
	var sPath = "/myPath";
	var oFilters = [];

	var filter = new Filter("Name", FilterOperator.EQ, "Fotios");
	oFilters.push(filter);

	var that = this;
	oModel.read(sPath, {
		filters: oFilters,
		success: function (oData, oResponse) {
			var data = new JSONModel(oData.results);
			that.getView().setModel(data, "Data");
                        console.log(that.getView().getModel("Data").getData()); //This prints the data
		},
			error: function (oError) {
			console.log(oError);
		}
	}).then(this.getView().byId("MyTable").setModel(data))


console.log(this.getView().getModel("Data").getData());
},

The console.log inside the success of the request is logging the data successfully.

The console.log at the end of the onInit function doesn't.

The .then() after the read() gives an error that it is not a function

I want to get the data as I do and either set it inside the success so I can use them in the View OR to use the .then() so I can do stuff after the data are in the component.

rohit_singhal
Active Contributor
0 Kudos

Hi Fotios,

If you bind the model to table control inside success callback method, do you face any issues?

Best Regards,

Rohit

View Entire Topic
junwu
Active Contributor
    YOURFUNCTIONNAME: function () {

                            return new Promise(function (resolve, reject) {

                        oModel.read(sPath, {
		filters: oFilters,
		success: function (oData, oResponse) {
			resolve(oData);
		},
			error: function (oError) {
			reject(oError);
		}
	})

                        },


// call YOURFUNCTIONNAME
this.YOURFUNCTIONNAME().then(*****)
0 Kudos
this.YOURFUNCTIONNAME().then(this.ANOTHERFUNCTION(*))

*How can I pass the data from the first function here?

junwu
Active Contributor
0 Kudos
resolve(oData);  this oData will be passed.