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.