What will `data` and `total` return if no documents meeting the criteria are found in the AnQiCMS document list?

Calendar 👁️ 114

When building a website or application with AnQiCMS, we often need to obtain various content through its provided API interface, such as document lists.When our query condition does not match any content, what kind of data structure will the API return? EspeciallydataandtotalThese two key fields are crucial for our proper data processing.Today, let's delve into how Anqi CMS responds when no documents meeting the criteria are found in the document list.

The API response when the document list query has no results

Start witharchive/listFor example, this interface is designed to retrieve document lists that meet specific conditions. When you pass in query parameters such ascategoryId/q(Search keyword) or custom filtering parameters, etc., when no documents match, the API of AnqiCMS will not return an error status code.On the contrary, it would consider this a successful query, but the result set is empty.

This means, you will seecodethe field is still0indicating that the request was processed successfully. Whilemsgthe field will usually be an empty string""Or sometimes it includes some default prompts that do not affect the logic, but the core idea is that there are no technical errors in the request itself.

What we really need to pay attention to is.datafield. In this case,datait will not returnnull, but it will return anempty array[]. This conforms to common practices in API design, namely,dataMaintain its type (here is an array), even if the array has no elements. For example, you might receive the following response snippet:

{
  "code": 0,
  "data": [],
  "msg": ""
}

This design allows us to directly traversedatathe array, if the array length is zero, we know that no documents were found, no additional check is neededdataIs itnull.

Next istotalField. According to the document description,totalonly whentype="page"(pagination mode) will it return. Therefore, if your request has settype="page"and no documents are found, thentotalthe field will return0It clearly tells us that although the request was successful, the total number of documents that meet the criteria is zero.

However, if you are using the defaulttype="list"pattern, or have not set it explicitlytypeThe parameter, so in the case where the document is not found,totalthe field willnot appear in the response. This means, inlistmode, the existence of the document is mainly determined by checkingdataThe array being empty is sufficient.

Advice on handling in actual development.

It is very important to understand this return mechanism. When you call the document list API of Anqie CMS, it is recommended that you first check.codeWhether the field is0to confirm whether the request is successful. Then, fordatathe field, directly judge whether its array length is0to know whether there is content. If you usetype="page"Performing pagination, you can directly obtain the total number of entries throughtotalthe field to get the total number of entries0information.

It is worth mentioning that this API response pattern of Anqi CMS is consistent with other list-type interfaces, such as getting the category list (category/list), Tag data list (tag/data/list), Even order list (orders), And others, when there is no matching result, theirdatafield will also be an empty array, andtotalField (if pagination is supported) will be0.

Mastered the standard return format of AnQi CMS when there is no query result, which can help us write more robust and elegant code, avoid unnecessary error judgments, and ensure the stable operation and good user experience of the website or application.


Frequently Asked Questions (FAQ)

  1. Q: Why even if the document is not found,codethe field is still0(Success) instead of an error code? A:The AnQi CMS API design treats "no documents found that meet the criteria" as a successful query, just that the result set is empty, not that the API service itself has occurred an error. Therefore,codeIt still indicates that the API request processing flow is normal.

  2. Q: IfdataIt returns an empty array[]How should I determine if there is any content? A:The most direct and recommended way is to checkdataThe length of the array. Ifdata.lengthWith0then it means nothing was found.

  3. Q:totalthe field always returns when there is no result?0? A:Not necessarily.totalthe field will only be returned when you explicitly set it.type="page"The parameter will appear in the response only when pagination is performed. In this case, if the query result is empty,totalwill return0. If not set,type="page"(i.e., using the default),type="list"), the response will not contain}totalfield.

Related articles

How to implement a 'Hot Articles' or 'Most Viewed' list, achieved through the `order` parameter?

In website operation, we all hope to display the most popular and highly read articles to visitors, such as common lists like "Hot Articles" or "Most Viewed".This not only effectively guides users to discover more exciting content and improve the user experience of the website, but it is also an indispensable part of content operation.In AnQi CMS, implementing such a feature is simpler than you imagine, the secret is hidden in the `archive/list` interface's `order` parameter.

2025-11-09

What is the default behavior and return content of the `archive/list` interface when no parameters are passed?

## In-depth Analysis of AnQiCMS `archive/list` Interface: Default Behavior and Return Content when No Parameters Are Provided When developing or managing websites with AnQiCMS, we often need to obtain various data through API interfaces.The `archive/list` interface is one of the core interfaces for obtaining the document list of the website.Understanding its default behavior and return content when no parameters are passed is crucial for efficient data acquisition and initial debugging.

2025-11-09

How to get the articles published by a specified user (`user_id`) through the AnQiCMS document list?

In website content operation, we often need to display all articles of a specific author, such as on the author's personal homepage, or on a special topic page aggregating the content of specific contributors.AnQiCMS (AnQiCMS) provides a powerful and flexible API interface, allowing us to easily meet this requirement. To get the list of all articles published by a specified user (`user_id`), we need to use the `archive/list` interface.

2025-11-09

What is the significance of the `price` and `stock` fields returned by the `archive/list` interface for e-commerce document models?

In AnQi CMS, when you retrieve the document list through the `archive/list` interface, the returned data includes the `price` and `stock` fields, which are of vital importance for constructing an e-commerce website model.They are not just simple numbers, but are the core elements supporting product display, transaction process, and inventory management.

2025-11-09

What is the help of `archive/list` interface returned `canonical_url` and `fixed_link` fields to SEO optimization?

In the ocean of website content, how can our high-quality content stand out and be discovered by more potential users, which is a topic that every content operator continuously explores.Search engine optimization (SEO) is one of the key strategies to achieve this goal.In SEO practice, the URL plays an extremely important role.

2025-11-09

How to use the `archive/list` interface to dynamically load more documents on the front end (infinite scrolling)?

In modern web design, infinite scrolling has become a popular content loading method that significantly enhances user experience, allowing visitors to maintain immersion while browsing content without interruption.For users who build websites using AnQiCMS, the `archive/list` interface is a powerful tool to achieve this function.

2025-11-09

Does the AnQiCMS document list interface support complex queries on the returned data's `extra` field?

In Anqi CMS, the flexibility of document content management is a highly关注的 feature, especially its support for custom fields (manifested in the `extra` field returned by the interface), providing great convenience for website operators.When we need to filter and query a large number of documents based on these custom properties, we naturally think of a key question: Does the Anqi CMS document list interface (`/api/archive/list`) support more complex queries on the `extra` field in the returned data?

2025-11-09

How to use the results of `archive/list` to implement click to view article details in conjunction with `archiveDetail.md`?

When building a website, displaying an article list and allowing users to click to view the details of the articles is a basic and core function.AnQiCMS (AnQiCMS) provides a powerful and flexible API interface, allowing us to easily achieve this requirement.Next, we will discuss how to use the `archive/list` interface to get an article summary and then use the `archive/detail` interface to display the full article content when the user clicks.

2025-11-09