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

The following 3 lines in the success solved the issue:

var data = new JSONModel();
data.setProperty("/List", oData.results);
that.getView().setModel(data, "Data")

And then the binding on the table was like this:

<Table id="idAvvisiTable" items="{Data>/List}">
...

<Text text="{Data>AnyPropery}>

Since this is an async function it is obvious why it doesn't console.log at the end of the onInit function. But how can I make use of the .then() after the .read() ?

junwu
Active Contributor
0 Kudos

your error msg already told you then is not a method, why you are still obsessed with that:)

0 Kudos

I understand that this is not the right way to use promises in SAPUI5. What I want to show is that I want to call the odata service and then() call something and finally() do something else. How can I use promises with calls?