cancel
Showing results for 
Search instead for 
Did you mean: 

get field from a JSON

former_member675984
Participant
0 Kudos
234

Hi, experts.

with the following code I printed this by console

var userModel = new sap.ui.model.json.JSONModel("/services/userapi/currentUser");
sap.ui.getCore().setModel(userModel, "userapi");
console.log(userModel);

How can I get the value of the name field?

Greetings from Colombia

Accepted Solutions (1)

Accepted Solutions (1)

FlorianVogt
Product and Topic Expert
Product and Topic Expert

Hi Andres,
you can do following steps:

First, create your model in the manifest.json: see https://blogs.sap.com/2016/10/11/sapui5-load-local-json-model-directly-manifest.json/

Storing a model in the Core is not recommended.

in your UI5 Controller:

onInit: function() {
  var oModel = this.getOwnerComponent().getModel("userModel");
  oModel.attachRequestCompleted(function(oEvent) {
    console.log(userModel.getProperty("/name"));
  });
}

Answers (1)

Answers (1)

junwu
Active Contributor
0 Kudos
userModel.getProperty("/name")
former_member675984
Participant
0 Kudos

hi, jun.wu5

implement it and print "undefined"

console.log(userModel.getProperty("/name"));<br>

What I can do?

junwu
Active Contributor

data loading is async, you cannot access it directly after the model creation.