javascript - 根据用户输入 ,"Uncaught TypeError: Cannot read property ' 调用 get 实体时读取未定义的

标签 javascript sapui5

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

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>

最佳答案

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

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

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

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

由于通常默认的 OData 模型是在没有名称 ("") 的情况下定义的,因此我建议您尝试这样调用它:

this.getView().getModel()

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

您可以在此处查看 manifest.json 文件的文档: https://sapui5.hana.ondemand.com/#/topic/be0cf40f61184b358b5faedaec98b2da.html

关于javascript - 根据用户输入 ,"Uncaught TypeError: Cannot read property ' 调用 get 实体时读取未定义的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52509100/

相关文章:

javascript - Cordova - 拒绝执行内联事件处理程序,因为它违反了以下内容安全策略

javascript - SAPUI5 表将操作绑定(bind)到图标模板

css - 如何在 UI5 应用程序中设置背景图像并使其响应?

javascript - SAPUI5:带参数的路由 - 如何获取绑定(bind)上下文的正确路径

javascript - 渲染后才聚焦?

javascript - 使用angularjs在 View 页面中拆分冒号

javascript - 这两者是否相同,或者其中一个比另一个更好/更安全?

javascript - 如果未选择另一组中的复选框,如何禁用一组复选框

javascript - 替换 Base64 - http/https 通信是否 8 位干净?

javascript - 每次显示页面时如何设置焦点