You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CAP v8升级后DateTime字段创建实体时触发错误求助

CAP v8升级后SAP Cloud SDK创建含DateTime字段实体报错问题

我在使用SAP Cloud SDK搭配SAP CAP时,从CAP v7升级到v8并重新生成OData客户端后,创建包含DateTime字段的实体时出现错误。

错误复现步骤

通过CAP示例项目bookshop复现该错误:

获取CAP示例应用

  • 获取CAP Cloud Samples示例项目
  • 将Bookshop应用的package.json第16行替换为"@sap/cds": ">=8",以升级CAP版本
  • 在Books实体的image字段后添加testField : DateTime;字段
  • 执行npm ci和cds watch bookshop启动CAP服务器

创建TypeScript Express应用

  • 创建简单的TypeScript Express应用并安装SDK依赖
  • 执行命令cds ../cloud-cap-samples/bookshop/srv/admin-service.cds -2 edmx > ./resources/bookshop.edmx获取bookshop admin服务的edmx文件(注意路径需对应)
  • 全局安装@sap-cloud-sdk/generator后,执行generate-odata-client --input resources/ --outputDir src/odata-clients/ --overwrite --clearOutputDir --optionsPerService resources/service-mapping.json生成OData客户端
  • 在index.ts中编写如下Get请求:
import { bookshop } from "./odata-clients/bookshop";
const { booksApi } = bookshop();

app.get("/post-books", async (req: Request, res: Response) => {
 const bookEntityToPost = booksApi
   .entityBuilder()
   .title("Test Book")
   .authorId(101)
   .descr("Test Description")
   .price(new BigNumber(100.0))
   .stock(10)
   .testField(moment.utc("2022-12-15 18:00:31.139000000")) // 从S/4HANA获取的示例时间字段
   .build();

 const craeteBookRequest = booksApi
   .requestBuilder()
   .create(bookEntityToPost)
   .skipCsrfTokenFetching();

 const result = await craeteBookRequest.execute({
   url: "http://localhost:4004",
   username: "alice",
   password: "",
   authentication: "BasicAuthentication",
 });

 console.log(result);

 res.send("Book posted");
});

运行应用

  • 执行ts-node src/main.ts启动应用,访问http://localhost:8000/post-books触发请求

错误输出

Node应用崩溃并报错:

[nodemon] starting `ts-node src/main.ts`
Server is Fire at http://localhost:8000
[2024-11-21T15:05:26.665Z] ERROR    ErrorWithCause: Create request failed!
    at /Users/path/to/project/node_modules/@sap-cloud-sdk/odata-common/src/request-builder/create-request-builder-base.ts:113:15
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
Caused by:
ErrorWithCause: post request to http://localhost:4004/admin failed! 
{"message":"Value 2022-12-15T18:00:31.139Z is not a valid DateTime","target":"testField","code":"400","@Common.numericSeverity":4}
    at constructError (/Users/path/to/project/node_modules/@sap-cloud-sdk/odata-common/src/request/odata-request.ts:291:12)
    at /Users/path/to/project/node_modules/@sap-cloud-sdk/odata-common/src/request/odata-request.ts:237:13
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
Caused by:
HTTP Response: Request failed with status code 400 - [object Object]
[nodemon] app crashed - waiting for file changes before starting...

CAP服务器日志:

[odata] - POST /admin/Books 
[cds] - [Value 2022-12-15T18:00:31.139Z is not a valid DateTime] {
  target: 'testField',
  args: [ '2022-12-15T18:00:31.139Z', DateTime { type: 'cds.DateTime' } ]
}
[error] - 400 > {
  message: 'Value 2022-12-15T18:00:31.139Z is not a valid DateTime',
  target: 'testField',
  code: '400',
  '@Common.numericSeverity': 4
}

编辑补充:已上传包含bookshop和SDK示例的仓库,README包含复现所需信息。


内容的提问来源于stack exchange,提问作者meakgoz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 05:45:00