typescript - 我如何在 LoopBack 4 模型中指定没有时间的日期?
我正在使用 LoopBack 4 构建一个 API,在一个模型中有一个名为“day”的属性,它的类型是 Date(MySQL 列的类型也是 Date)。
但是我不能向它发送像 "2019-09-09" 这样的值,因为它需要类似 "2019-09-09T12:41:05.942Z" 的格式。我怎样才能指定它必须是日期(不包含时间)?
我感到困惑,因为你可以在查询参数(类型为日期)中传递 "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:日期应符合“日期-时间”格式
答案#1
我遇到了同样的问题,我通过使用 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
答案#2
类型应该改为'date'而不是Date。这将允许更灵活的日期模式:
@property({
type: 'date',
required: true,
mysql: {
columnName: 'day',
dataType: 'date',
dataLength: null,
dataPrecision: null,
dataScale: null,
nullable: 'N',
},
})
day: string;
