Skip to Content
0

Uncaught TypeError: Cannot read property 'setValue' of undefined

Mar 09, 2017 at 04:13 PM

1.3k

avatar image

I can't set a value in my input.

When I try do it the console shows error "Uncaught TypeError: Cannot read property 'setValue' of undefined"

The error is in the line "sap.ui.getCore().byId("InputValueHelp").setValue(oSelectedItem.getTitle());"

My Controller code is bellow

sap.ui.define([
		'jquery.sap.global',
		'sap/ui/core/Fragment',
		'sap/ui/core/mvc/Controller',
		'sap/ui/model/Filter',
		'sap/ui/model/json/JSONModel'
	], function(jQuery, Fragment, Controller, Filter, JSONModel) {
	"use strict";


	var CController = Controller.extend("sap.m.sample.InputStates.C", {
		
		onInit: function () {
			// set explored app's demo model on this sample
		
		},


	valueHelpRequest: function(oEvent) {


        var model = new sap.ui.model.json.JSONModel();
        var newurl = "http://services.odata.org/Northwind/Northwind.svc/Categories";
        model.loadData(newurl, null, false, "GET", false, false, null);
        // Handling of both confirm and cancel; clear the filter
        var handleClose = function(oEvent) {
          var oSelectedItem = oEvent.getParameter("selectedItem");
          if (oSelectedItem) {
          	 sap.ui.getCore().byId("InputValueHelp").setValue(oSelectedItem.getTitle());
            }
          oEvent.getSource().getBinding("items").filter([]);
        };
        // Create a SelectDialog and display it; bind to the same
        // model as for the suggested items
        if (!this._valueHelpSelectDialog) {
          this._valueHelpSelectDialog = new sap.m.SelectDialog("valueHelpSelectDialog", {
            title: "Categories",
            items: {
              path: "/value",
              template: new sap.m.StandardListItem({
                title: "{CategoryName}",
                active: true
              })
            },
            search: function(oEvent) {
              var sValue = oEvent.getParameter("value");
              var oFilter = new sap.ui.model.Filter(
                "CategoryName",
                sap.ui.model.FilterOperator.Contains, sValue
              );
              oEvent.getSource().getBinding("items").filter([oFilter]);
            },
            confirm: handleClose,
            cancel: handleClose
          });


          this._valueHelpSelectDialog.setModel(model);


        } else {
          this._valueHelpSelectDialog.setModel(model);
        }
        this._valueHelpSelectDialog.open();


      }
	});




	return CController;


});
ui5
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

4 Answers

Best Answer
Jun Wu Mar 09, 2017 at 04:42 PM
1

var that=this;

var handleClose =function(oEvent){

          var oSelectedItem = oEvent.getParameter("selectedItem");if(oSelectedItem){that.byId("InputValueHelp").setValue(oSelectedItem.getTitle());}
          oEvent.getSource().getBinding("items").filter([]);};
Show 3 Share
10 |10000 characters needed characters left characters exceeded

Now it's work.

I have another doubt,if I cosume a Odata service with username and password, where I put the password and the username in the method model.loadData()?

Thank you

0

please accept this as answer.thanks

0
0
Jun Wu Mar 09, 2017 at 04:36 PM
1

this.byId("InputValueHelp").setValue(oSelectedItem.getTitle());

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Now the error is "Uncaught TypeError: this.byId is not a function"

0
Jun Wu Mar 09, 2017 at 04:41 PM
1

show me the view, and your ui5 version

Share
10 |10000 characters needed characters left characters exceeded
Srikanth KV Mar 09, 2017 at 05:04 PM
0

Id of UI control is always prefixed with view id. At runtime it could be like '__xmlvie0--InputValueHelp', so you need to generate this id and access it

Store controller reference in a global variable from 'init' method

sap.ui.define([
		'jquery.sap.global',
		'sap/ui/core/Fragment',
		'sap/ui/core/mvc/Controller',
		'sap/ui/model/Filter',
		'sap/ui/model/json/JSONModel'
	], function(jQuery, Fragment, Controller, Filter, JSONModel) {
	"use strict";

         var _oController;   //variable to store controller reference         
         
	var CController = Controller.extend("sap.m.sample.InputStates.C", {
		
		onInit: function () {
			// set explored app's demo model on this sample
		       _oController = this;  ///set controller instance to global variable
		},

Now changes your code to access UI element to below:

var sId = _oController.getView().createId("InputValueHelp");   //this creates id like '__xmlview0--InputValueHelp
sap.ui.getCore().byId(sId).setValue(oSelectedItem.getTitle());

            (or)

_oController.getView().byId("InputValueHelp").setValue(oSelectedItem.getTitle());
Show 4 Share
10 |10000 characters needed characters left characters exceeded

Now it's work.

I have another doubt,if I cosume a Odata service with username and password, where I put the password and the username in the method model.loadData()?

Thank you

0

In oData Model instantiation there is a option to set user id and password

0

Can you show me a example?

0

manifest.json file, under sap.ui5 --> models

		"models": {
			"serverModel": {
				"type": "sap.ui.model.odata.v2.ODataModel",
				"settings": {
					"user": <user id>,
					"password": <password>
				},
				"dataSource": "testModel",
				"preload": true
			}
		}

In a production scenario this is not advised, server should take care of this.

0