typescript - 如何在 LoopBack 4 模型中指定没有时间的日期?

标签 typescript loopback4

我正在使用 LoopBack 4 构建一个 API,在模型中有一个名为“day”的属性,它是日期类型(MySQL 列也是日期类型)。

但我无法向其发布诸如“2019-09-09”之类的值,因为它需要诸如“2019-09-09T12:41:05.942Z”之类的值。 我如何指定它必须是日期(没有时间)?

我很困惑,因为您可以在查询参数(日期类型)中传递“2019-09-09”,但不能在模型中传递“2019-09-09”。

我目前在模型中拥有如下属性:

@property({
    type: Date,
    required: true,
    mysql: {
        columnName: 'day',
        dataType: 'date',
        dataLength: null,
        dataPrecision: null,
        dataScale: null,
        nullable: 'N',
    },
})
day: Date;

预期:接受“2019-09-09”作为值

实际上:422:日期应该匹配格式“日期时间”

最佳答案

我遇到了同样的问题,我使用 jsonSchema 指定请求 JSON 的格式解决了它。

就您而言,您必须通过以下方式更改代码:

@property({
    type: 'date', // Types in LoopBack4 are not case-sensitive so Date is the same than date
    jsonSchema: { 
      format: 'date', //This can be changed to 'date-time', 'time' or 'date'
    },
    required: true,
    mysql: {
        columnName: 'day',
        dataType: 'date',
        dataLength: null,
        dataPrecision: null,
        dataScale: null,
        nullable: 'N',
    },
})
day: string; // Change this also

关于typescript - 如何在 LoopBack 4 模型中指定没有时间的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57854413/

相关文章:

typescript - 如何获取嵌套对象的类型推断?

node.js - Loopback 4不会生成构建文件

node.js - 下载带有环回的文件 4

node.js - Loopback 4 POST 方法给出 422 错误

typescript - 如何在 VS Code 上使用 typescript 调试 feathersjs?

javascript - 在 TypeScript 中凝胶化一个类的所有模型属性

javascript - NestJs 基于用户属性和策略的 CASL 授权守卫

javascript - 在 Typescript 中以编程方式折叠 Bootstrap 导航栏

javascript - 使用 Loopback 4 发送电子邮件

javascript - 生成: true not working in loopback 4