1

I need to retrieve the property of my JSONModel in my controller. The scenario is as follows:

I have a "questionnaire" model (questionnaire.json) which contains questions and answers. Then I have a controller (App.controller.js). In this controller, I have created one more model in onInit with the name "currentQuestion" because I need to implement a scenario where the next question should be loaded when the user clicks on the next button. I am trying to get the property of my "questionnaire" model but I am facing an error:

Cannot read property of 'getProperty' of undefined.

You can see the model data and my controller here:

questionnaire.json

{
    "data": [{
        "question": "Which pet do you like from the following?",
        "answers": ["Cats", "Rabbits", "Dogs", "Hamsters"],
        "correct": 1
    }, {
        "question": "Which pet do you prefer from the following?",
        "answers": ["Cats", "Dogs"],
        "correct": 1
    }, {
        "question": "What food brand does your pet eat?",
        "answers": ["Pedigree", "Catfood", "Rabbitfood", "HamstersFood"],
        "correct": 1
    }]
}

App.controller.js

sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/model/json/JSONModel"
], function(Controller, JSONModel){

    Controller.extend("opensap.onlinequestionnaire.controller.App", {
        onInit: function() {
            this.questionSetUp();
            this.getView().setModel(new JSONModel({
                    index: false,
                    question: {}
            }), "currentQuestion");

            var oCurrentQuestionModel = this.getView().getModel("currentQuestion");
            var oQuestionModel = this.getView().getModel("questionnaire");
            var iCurrentIndex = oCurrentQuestionModel.getProperty("/index");
            var oQuestion = oQuestionModel.getProperty("/data/0/question");
            console.log(oQuestion);
            console.log(iCurrentIndex);
            iCurrentIndex = iCurrentIndex ? ++iCurrentIndex : 0;
            console.log(iCurrentIndex);
        }
    });
});

When I try to question property in oQuestion, it throws me the error. How do I get the 0th element's question property's value?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
loki
  • 648
  • 18
  • 37

3 Answers3

1

One more thing I was doing wrong was I was trying to get this model in init. [src]

Unfortunately yes. That was actually the main reason why the model was undefined in this case. But not because the model was not loaded. We know from the documentation that models defined in the app descriptor (manifest.json) are set to the component and that those models get propagated to component's children. Hence, calling view.getModel from any controller should return the model seamlessly because it's set globally on the component. But: Something that is not obvious is that models set on the component cannot be retrieved in onInit of any Controller just by using the corresponding view because the view doesn't know its parent yet in that state. Calling view.getParent in onInit returns null. So the component model "questionnaire" was not propagated to your view yet. As you did it already, such component models can be easily retrieved in onInit by using the component instead of the view: this.getOwnerComponent().getModel("modelName") and the error

Cannot read property of 'getProperty' of undefined

is gone.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
0

the method setModel(oModel, sName?) sets a model to your view. In your code you set a Model with the name "currentQuestion" to the view which contains the data from the questionnaire.json file. You get this model in the variable oCurrentQuestionModel. Please try to get the property from this model by the following code: oCurrentQuestionModel.getProperty("/data/0/question"); In your code snippet it is not clear where oQuestionModel is defined.

  • oQuestionModel get the Model questionnaire.json which is my json named model in manifest. – loki May 12 '17 at 10:47
  • Can you post the manifest.json and the error message ? – Henning Rettenmaier May 12 '17 at 10:59
  • I solved it I needed to use the `var oQuestionModel = this.getOwnerComponent().getModel("questionnaire")` to get my named model and then i retrieved the property with getProperty as follows: `oQuestionModel.getProperty("/data/0/question")` One more thing I was doing wrong was I was trying to get this model in init() I think the reason it gave me undefined because the model was not loaded. So I called it in different method and I got the result. Thank you for trying to help. – loki May 12 '17 at 11:03
  • Normally you define the model in the manifest.json and then you can easily access it via: `oController.getView().getModel(nameInManifest.JSON)` – Henning Rettenmaier May 12 '17 at 11:05
0

In your case. I think you should change

var oQuestionModel = this.getView().getModel("questionnaire");

to

var oQuestionModel = this.getView().getModel();

because you did not name your model so that it becomes the default model.

emch2
  • 211
  • 2
  • 11