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?