首页 > 解决方案 > 如何删除“Strapi”中具有特定值的条目?

问题描述

在 headless-cms Strapi 中的某些情况下删除条目的正确方法是什么?如果一种内容类型获得特定值。

例如,如果日期已到/已过期。就我而言,我创建了一个活动日历。它有一些内容类型,如标题、位置和日期。到达事件日期后,每个事件都必须自动消失。我怎样才能做到这一点?

标签: databasecontent-management-systemstrapiheadless-cms

解决方案


我建议你使用 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
}));

推荐阅读