on 2013 Oct 17 3:32 PM
Hi,
I have the following code:
var oModel = new sap.ui.model.json.JSONModel();
oModel.loadData("credentials.json");
var user = oModel.getProperty("/credentials/benutzer");
And the following JSON file:
{
"credentials": {
"benutzer": "test",
"passwort": "test"
}
}
When I say now alert(user) I always get "undefined" ...
Does anybody now why it doesn't work?
Greetings
Stef
Request clarification before answering.
I am facing the similar issue some one please help me with the issue in my code.
Model File:
sap.ui.define([
], function () { 'use strict'; return { createJSONModel: function (sPath) { var oModel = new sap.ui.model.json.JSONModel(); oModel.loadData(sPath); return oModel; } }Controller File: onFlip() function
onFlip: function () { var oModel1 = oModels.createJSONModel('model/mockdata/mydata.json'); var oEmpStr1 = oModel1.getProperty('/empstr');
The var oEmpStr1 = oModel1.getProperty('/empstr'); is giving undefined
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
loadData() returns a promise. You can get that promise and wait for it to be resolved/rejected. As Brian mentioned, loadData can take some time and the getProperty gets called before the model is loaded.
May you try this await syntax or just use standard promise then/catch handling:
createJSONModel: async function (sPath) {
var oModel = new sap.ui.model.json.JSONModel();
try{
var res = await oModel.loadData(sPath); // await the promise to be finished
return oModel;
}
catch (e){
// error handling if data loading failed with return
}
}
<br>
Hi,
it works fine with the .attachRequestCompleted Event. But now comes another question. Am I able to update the json source file? I only have one entry in it:
{
"credentials": {
"user": "test",
"password": "test"
}
}
and now I want to set user and password to the value which the user enters. Is this possible?
Looking forward to your answers
Greetings and thanks!
Stef
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
this could be a matter of "timing". When calling the "loadData" method, you trigger an async request. This means, that when you try to get a property from the model there is no guarantee that the data is loaded already. You might consider registering an event handler on the "RequestCompleted" event of the model. Please refer to the API reference for more details.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
72 | |
30 | |
8 | |
7 | |
6 | |
6 | |
6 | |
4 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.