当基于用户输入调用get实体时,“ Uncaught TypeError:无法读取未定义的属性'read'

时间:2018-09-26 02:28:43

标签: javascript sapui5

我试图根据用户在UI5屏幕上的输入来调用oData来读取值。 我在UI5屏幕上输入物料凭证编号和会计年度。但是当它执行语法this.getView().getModel("OData").create("/UserInputSet", oViewModel,时,它说

  

未捕获的TypeError:无法读取未定义的属性“读取”。

控制器:

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 个答案:

答案 0 :(得分:2)

通过调用this.getView().getModel("OData"),您正在尝试获取名为“ OData”的模型的实例。如果返回undefined,则表示此模型未定义。

通常,OData模型在manifest.json文件中定义,并绑定到dataSource。像这样:

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

""表示它是默认模型,无需指定名称即可访问。

因为通常情况下,默认OData模型是不带名称("")定义的,所以建议您尝试这样命名:

this.getView().getModel()

如果这不起作用,请修改manifest.json文件并包含模型定义。

您可以在以下位置检查docs中的manifest.json文件: https://sapui5.hana.ondemand.com/#/topic/be0cf40f61184b358b5faedaec98b2da.html