Skip to main content

文档服务 API:草稿与发布的使用

🌐 Document Service API: Usage with Draft & Publish

默认情况下,当启用 Draft & Publish 功能时,Document Service API 会返回文档的草稿版本。此页面描述了如何使用 status 参数来:

🌐 By default the Document Service API returns the draft version of a document when the Draft & Publish feature is enabled. This page describes how to use the status parameter to:

  • 返回文档的已发布版本,
  • 根据文档的状态计数文档,
  • 并在创建或更新文档时直接发布文档。
Note

{ status: 'draft' } 传递给文档服务 API 查询返回的结果与未传递任何 status 参数时相同。

🌐 Passing { status: 'draft' } to a Document Service API query returns the same results as not passing any status parameter.

获取已发布的版本与 findOne()

🌐 Get the published version with findOne()

findOne() 查询默认返回文档的草稿版本。

要在使用文档服务 API 查找特定文档 时返回已发布的版本,请传递 status: 'published'

🌐 To return the published version while finding a specific document with the Document Service API, pass status: 'published':

Example request
await strapi.documents('api::restaurant.restaurant').findOne({
documentId: 'a1b2c3d4e5f6g7h8i9j0klm',
status: 'published'
});
Example response
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant",
publishedAt: "2024-03-14T15:40:45.330Z",
locale: "en", // default locale
// …
}

获取带有 findFirst() {#find-first} 的已发布版本

🌐 Get the published version with findFirst()

findFirst() 查询默认返回文档的草稿版本。

要在使用文档服务 API查找第一个文档时返回已发布的版本,请传递 status: 'published'

🌐 To return the published version while finding the first document with the Document Service API, pass status: 'published':

示例请求
const document = await strapi.documents("api::restaurant.restaurant").findFirst({
status: 'published',
});
示例响应
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant",
publishedAt: "2024-03-14T15:40:45.330Z",
locale: "en", // default locale
// …
}

获取已发布的版本与 findMany()

🌐 Get the published version with findMany()

findMany() 查询默认返回文档的草稿版本。

在使用文档服务 API 查找文档 时,要返回已发布的版本,请传递 status: 'published'

🌐 To return the published version while finding documents with the Document Service API, pass status: 'published':

示例请求
const documents = await strapi.documents("api::restaurant.restaurant").findMany({
status: 'published'
});
示例响应
[
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant",
publishedAt: "2024-03-14T15:40:45.330Z",
locale: "en", // default locale
// …
}
// …
]

count() 仅草稿或已发布版本

🌐 count() only draft or published versions

在使用文档服务 API 计数文档 时,如果只考虑文档的草稿或已发布版本,请传递相应的 status 参数:

🌐 To take into account only draft or published versions of documents while counting documents with the Document Service API, pass the corresponding status parameter:

// Count draft documents (also actually includes published documents)
const draftsCount = await strapi.documents("api::restaurant.restaurant").count({
status: 'draft'
});
// Count only published documents
const publishedCount = await strapi.documents("api::restaurant.restaurant").count({
status: 'published'
});
Note

由于已发布的文档必然也有草稿副本,因此已发布的文档仍算作具有草稿版本。

🌐 Since published documents necessarily also have a draft counterpart, a published document is still counted as having a draft version.

这意味着,即使某些文档已经发布并且在内容管理器中不再显示为“草稿”或“已修改”,使用 status: 'draft' 参数进行计数仍会返回符合其他参数的文档总数。目前没有方法可以阻止已发布的文档被计入统计。

🌐 This means that counting with the status: 'draft' parameter still returns the total number of documents matching other parameters, even if some documents have already been published and are not displayed as "draft" or "modified" in the Content Manager anymore. There currently is no way to prevent already published documents from being counted.

创建草稿并发布它

🌐 Create a draft and publish it

要在创建文档时自动发布它,请将 status: 'published' 添加到传递给 create() 的参数中:

🌐 To automatically publish a document while creating it, add status: 'published' to parameters passed to create():

Example request
await strapi.documents('api::restaurant.restaurant').create({
data: {
name: "New Restaurant",
},
status: 'published',
})
Example response
{
documentId: "d41r46wac4xix5vpba7561at",
name: "New Restaurant",
publishedAt: "2024-03-14T17:29:03.399Z",
locale: "en" // default locale
// …
}

更新草稿并发布它

🌐 Update a draft and publish it

要在更新文档时自动发布它,请将 status: 'published' 添加到传递给 update() 的参数中:

🌐 To automatically publish a document while updating it, add status: 'published' to parameters passed to update():

Example request
await strapi.documents('api::restaurant.restaurant').update({
documentId: 'a1b2c3d4e5f6g7h8i9j0klm',
data: {
name: "Biscotte Restaurant (closed)",
},
status: 'published',
})
Example response
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant (closed)",
publishedAt: "2024-03-14T17:29:03.399Z",
locale: "en" // default locale
// …
}