1
votes

I was trying to call the oData to read the values based on user input on UI5 screen. I am entering Material doc no and fiscal year on UI5 screen. But when it execute the syntax this.getView().getModel("OData").create("/UserInputSet", oViewModel, , it says

Uncaught TypeError: Cannot read property 'read' of undefined.

Controller :

onNext : function(){
            debugger;
            var oViewModel = {};
            oViewModel.Mblnr = this.getView().byId("idMd").getValue();
            oViewModel.Mjahr = this.getView().byId("idFy").getValue();
            if (oViewModel.Mblnr === "") {
                // alert('Please Enter the material Doc No');
                var msg = ("Please Enter the material Doc No.");
                MessageToast.show(msg);
            } else
            if (oViewModel.Mjahr === "") {    
                msg = ("Please Enter the fiscal year");
                MessageToast.show(msg);
            } else {    
                this.getView().getModel("OData").read("/UserInputSet", oViewModel, {success: function(OData, response) {    
                    MessageToast.show("Material No exist in table");                                    
                    },error: function(OData, response) {        MessageToast.show("Error");

           }

XML

<Page id="idFirst" title="Update Picking Completion App ">
    <headerContent>
        <Button press="onNext" icon="sap-icon://feeder-arrow" iconFirst="false" text="Execute" tooltip="Execute" type="Accept"
            class="customCss_ForButton"/>
    </headerContent>
    <f:SimpleForm id="idSimple" title="User Input screen" editable="true" layout="ResponsiveGridLayout" labelSpanXL="4" labelSpanL="4"
        labelSpanM="4" labelSpanS="4" adjustLabelSpan="false" emptySpanXL="4" emptySpanL="4" emptySpanM="4" emptySpanS="0" columnsXL="1"
        columnsL="1" columnsM="1" singleContainerFullSize="true">
        <f:content >
            <Label text="Material Doc No"/>
            <Input id="idMd" placeholder="Enter material doc ..." width="auto" maxLength="10" required="true">
                <layoutData>
                    <l:GridData span="XL1 L2 M2 S4"/>
                </layoutData>
            </Input>
            <Label text="Fiscal Year"/>
            <Input id="idFy" type="Text" placeholder="Enter fiscal year ..." width="auto" maxLength="4" required="true">
                <layoutData>
                    <l:GridData span="XL1 L2 M2 S4"/>
                </layoutData>
            </Input>
            <Button text="Execute" width="150px" icon="sap-icon://feeder-arrow" iconFirst="false" press="onNext" type="Accept"
                class="customCss_ForButton"/>
        </f:content>
    </f:SimpleForm>
</Page>
1
at a guess, this.getView().getModel("OData") is undefined - the question is, why is it undefined when you don't expect it to be undefined - Jaromanda X
of course, the code you've posted is incomplete - i.e. missing }) } } at the end of the code, for example - and very poorly indented - Jaromanda X
Is the model name "OData" bound to the view. I suspect that is the issue. - Nandan Chaturvedi

1 Answers

2
votes

By calling this.getView().getModel("OData") you're trying to get the instance of a model called "OData". If that's returning undefined it means this model is not defined.

Usually OData models are defined in the manifest.json file, bound to a dataSource. Like this:

{
    "sap.app": {
        "dataSources": {
            "mainService": {
                "uri": "/relative/uri/of/the/service",
                "type": "OData"
            }
        }
    },
    "sap.ui5": {
        "models": {
            "": {
                "dataSource": "mainService"
            }
        }
    }
}

The "" means it's the default model, and it can be accessed without specifying a name.

Since normally the default OData model is defined without a name ("") I suggest you try to call it like this:

this.getView().getModel()

If that doesn't work, revise your manifest.json file and include a model definition.

You can check the docs for the manifest.json file here: https://sapui5.hana.ondemand.com/#/topic/be0cf40f61184b358b5faedaec98b2da.html