我已经使用了为较低版本提供的look钩子,但我想知道如何通过模型添加时间戳createdAt和updatedAt,而不需要与控制器中的时间戳进行交互。
我曾经在Rails上工作过,但Loopback-4对我来说是新事物,没有Rails那么灵活。我只需要几个特定型号的时间戳
发布于 2020-08-06 15:09:40
你可以用以下方法做到这一点:
模型文件中的:
@property({
type: 'date',
default: () => new Date()
})
created ? : string;
@property({
type: 'date',
default: () => new Date()
})
modified ? : string;存储库文件中的:
constructor(
@inject('datasources.db') dataSource: DbDataSource,
) {
super(User, dataSource);
(this.modelClass as any).observe('persist', async (ctx: any) => {
ctx.data.modified = new Date();
});
}https://stackoverflow.com/questions/62451254
复制相似问题