简体   繁体   English

SAP UI5 oData2模型对非关键字段错误使用读取方法

[英]SAP UI5 oData2 Model use of read method on non-key field error

I am trying to read data from Employee table using SAP UI5 and oData 2 Model. 我正在尝试使用SAP UI5和oData 2模型从Employee表读取数据。 I am using Read method of oData model with primary key and it is working well but when I am trying to use with non key field then it is getting error with message 我正在使用具有主键的oData模型的Read方法,它运行良好,但是当我尝试与非键字段一起使用时,它会出现错误消息

No key property 'Employee_Name' exists 不存在关键属性“ Employee_Name”

.

 oModel.read("/Employee_Service(Employee_ID=2)", { success: function(oData, response) { alert("Success!"); }, error: function(response) { alert("Error"); } 

Here Employee_ID is primary key and this code is running well. 这里Employee_ID是主键,此代码运行良好。

oModel.read("/Employee_Service(Employee_Name='Bob')", {
success: function(oData, response) {
alert("Success!");
},
error: function(response) {
alert("Error");
}

This code is getting error as said earlier. 如前所述,此代码出错。

Can you please help me to solve the issue? 您能帮我解决问题吗?

Only properties which are assigned as Primary key can be accessed in this way: 只能以这种方式访问​​分配为主键的属性:

Employee_Service(Employee_ID=2)

To access records with other properties you can use $filter query option. 要访问具有其他属性的记录,可以使用$filter查询选项。

The $filter system query option allows clients to filter a collection of resources that are addressed by a request URL. $ filter系统查询选项允许客户端过滤由请求URL寻址的资源的集合。 The expression specified with $filter is evaluated for each resource in the collection, and only items where the expression evaluates to true are included in the response. 将为集合中的每个资源评估由$ filter指定的表达式,并且响应中仅包括该表达式的评估结果为true的项目。

So your URL will be like this: 因此,您的网址将如下所示:

"/Employee_Service?$filter=Employee_Name eq 'Bob'"

I found an article which helps me a lot to solve the issue. 我找到了一篇文章,对解决这个问题有很大帮助。 Which I was trying this should work but I do not know why it is getting error. 我试图这样做应该可以,但是我不知道为什么它会出错。 However if any one can help me to find answer of my first approach (as above) always welcome. 但是,如果有人可以帮助我找到第一种方法的答案(如上所述),请随时欢迎。 Here is the solution which I did. 这是我所做的解决方案。

var la_filters = new Array();
var lv_Filter = new sap.ui.model.Filter({
path: "BOWTIE_CLASS",
operator: sap.ui.model.FilterOperator.EQ,
value1: bowtieClass
});

la_filters.push(lv_Filter);

oModel.read("/Employee_Service", {
filters: la_filters,
success: function(oData, response) {
alert("Success");
},
error: function(response) {
alert("Error");

}

Hint URL I found here https://scn.sap.com/thread/3781251 我在这里找到的提示URL https://scn.sap.com/thread/3781251

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM