Getting a non-array response when querying documents by a unique field (route) in the REST API

default discord avatar
Lucyalast year
2

Hello, I am trying to query Payload's REST API to get a single page by it's "route" field. Route is a unique, indexed field. The goal is to pass along whatever route the user is trying to navigate to on our front end to Payload's API, then render that page based on the document returned.



My current API request looks like this:

/api/pages?where[route][equals]=%2Fsomeroute&limit=1

. It works completely fine but the response I get back is inside a "docs" array and has a bunch of pagination information I do not need. Example below:


{
    "docs":
    [
        {
            "id": "6446ffa4a3f516bcf048241a",
            "name": "Page Name",
            "route": "/someroute",
            "publishedDate": "2023-04-24T04:00:00.000Z",
            "_status": "published",
            "createdAt": "2023-04-24T22:16:04.168Z",
            "updatedAt": "2023-04-25T20:26:12.931Z",
        }
    ],
    "totalDocs": 1,
    "limit": 1,
    "totalPages": 1,
    "page": 1,
    "pagingCounter": 1,
    "hasPrevPage": false,
    "hasNextPage": false,
    "prevPage": null,
    "nextPage": null
}


It is not that I cannot work with this, but it would really be much better if there was a way for me to get just the document itself as I am filtering by a unique field and will never get more than one document back. I am working with a strongly types language (C#) for our actual website so this data format makes serialization to the actual model pretty annoying because I need to tell the code to expect to serialize an array "docs" and only get the first element every time.



Thanks for the help!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.