在某些情况下,在headless-cms Strapi中删除条目的正确方法是什么?如果一个内容类型获得了一个特定值。
例如,如果该日期已到达/过期。在我的例子中,我创建了一个事件日历。它有一些内容类型,比如标题、位置和日期。到达事件日期后,每个事件必须自动消失。我如何才能做到这一点?
发布于 2019-09-04 17:32:05
我建议你使用CRON任务。此功能在Strapi中可用
这是文档https://strapi.io/documentation/3.0.0-beta.x/configurations/configurations.html#cron-tasks。
因此,通过遵循文档创建您的CRON任务,然后在函数中,您将必须获取日期早于CRON函数执行日期的数据。
为此,您可以使用查询函数。
// Fetch data that have the `yourDateAttributeName_lt` lower than the now.
const data = await strapi.query('article').find({
yourDateAttributeName_lt: new Date()
});
// Delete all entries fetched.
data.forEach((entry) => strapi.query('article').delete({
id: entry.id
}));发布于 2021-11-08 15:45:58
我建议阅读有关自定义查询https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#custom-queries的内容
例如,使用knex delete可能很容易:
await strapi.connections.default('emails').where('isSend', true).del();有关定期任务的信息,请参阅如何使用CRON https://strapi.io/documentation/developer-docs/latest/guides/scheduled-publication.html
https://stackoverflow.com/questions/57686669
复制相似问题