0

我们正在尝试通过 odata 服务创建具有日期属性的实体。后端是一个 sap 系统。这个实体只有 3 个关键属性加上一堆其他属性。我们已经确定密钥中的日期是问题的根本原因。

键:

  • pernr 类型字符串,
  • begda类型日期时间
  • endda 类型日期时间。

下面的代码(不起作用)在尝试解决问题时已被大大简化。目前,它从实体集中读取一个实体,并立即尝试创建一个具有完全相同数据的实体。

代码:

var oODataModel = new sap.ui.model.odata.ODataModel("/sap/opu/odata/sap/Z_PERSONAL_DATA_SRV/");

//Test entity to be saved
var entity = null;

//Handler for read error
var handleReadE = function (oEvent){ 
    alert("error");
};

//Handler for read success
var handleRead = function (oEvent){
    //Get the data read from backend
    entity = oEvent.results[0];

    //Try to create a new entity with same data
    oODataModel.create('/PersDataSet', entity, null, function(){
        alert("Create successful");
    },function(oError){
        alert("Create failed", oError);
    });
};

oODataModel.read("/PersDataSet", null, [], true, handleRead, handleReadE);

在网关错误日志中,出现 xml 解析错误。在这个日志中,我们可以看到请求数据,可以看到日期是用String类型传输的。这些日期在服务中定义为 DateTimes,因此请求被拒绝。例子:

<m:properties>
<d:Pernr m:type="Edm.String">00000001</d:Pernr>
    <d:Endda m:type="Edm.String">9999-12-31T00:00:00</d:Endda>
    <d:Begda m:type="Edm.String">1979-05-23T00:00:00</d:Begda>

读取实体时,后端不发送任何类型信息。它发送如下示例:

<m:properties>
   <d:Pernr>72010459</d:Pernr>
   <d:Endda>9999-12-31T00:00:00</d:Endda>
   <d:Begda>1876-07-21T00:00:00</d:Begda>

而且,事实上,如果我们尝试在没有 type=".." 的情况下保存相同的信息,它会起作用。所以问题是 ODataModel.create 添加到 xml 的类型不正确。

我的问题是:我可以告诉 ODataModel.create 不要添加此类型信息吗?在推断类型方面做得不好。

任何人都可以通过 odata 分享一个读取和写入日期的示例吗?

非常感谢您提前。

4

2 回答 2

2

从 oODataModel.read 返回的数据是原始的,在你发布之前你需要解析它

 var handleRead = function (oEvent){
   //Get the data read from backend
   entity = oEvent.results[0];

   var newEntity = jQuery.extend({},entity);
   delete newEntity.__metadata;
   newEntity.Begda = new Date(entity.Begda);
   newEntity.Endda = new Date(entity.Endda);

   //Try to create a new entity with same data
   oODataModel.create('/PersDataSet', newEntity, null, function(){

为什么不使用 json 而不是 xml?

于 2014-05-22T06:09:51.423 回答
0

Thanks all for the help. We got this working accounting for the following:

The problem of the wrong types appended to the attributes comes from the read itself. The object returned by read has a __metadata attribute which describes the values. In this object the dates are set with type=edm.string, even when the service says they are DateTime. To me this is a bug of the .read function.

When trying to use the same object to save, create sees the __metatada on the entry and uses those values, producing type edm.string type for the dates. This caused the request to be rejected. Manually changing these __metadata.properties...type to Edm.DateTime makes it work.

In the end, we did the following:

  • Dates are parsed manually from the Odata response, creating a js Date object from the strings in format "yyyy-mm-ddT00:00:00", to make it work with control bindings. When we want to save, the reverse is done.
  • The object to be created is a new object with only the attributes we care (no __metadata)
于 2014-05-22T13:37:04.007 回答