👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

How to return both the title and value of an object in Sanity

9 replies
Last updated: Dec 7, 2022
Hello, I am returning an object from sanity that contains a list. The list contains title and value options. When the data is fetched, only the value is returned. How do I return both the title and value?
Dec 7, 2022, 11:55 AM
Please do show some code, much easier to help 😄
Dec 7, 2022, 11:59 AM
Schema:

{
name: ‘type’,
title: ‘Type:’,
type: ‘string’,
options: {
list: [
{
title: “Plant Seeds”,
value: “plant-seeds”
},
{
title: “Initial Tower Setup”,
value: “initial-tower-setup”
},
{
title: “Complete Tower Setup”,
value: “complete-tower-setup”
},
{
title: “Transplant”,
value: “transplant”
},
{
title: “Maintenance”,
value: “maintenance”
},
{
title: “Harvest”,
value: “harvest”
},
{
title: “Teardown”,
value: “teardown”
},
]
},
validation: (Rule) => Rule.required(),
},
Dec 7, 2022, 12:02 PM
Query:
`const query = `*[_type=='gardenEvent' && references('${getActiveProgram()._id}')]``
Dec 7, 2022, 12:02 PM
Only returns the value, not the title
Dec 7, 2022, 12:02 PM
Can you take a screenshot of what you get in vision with that query?
Dec 7, 2022, 12:13 PM
[

{

"_createdAt": "2022-12-06T19:46:26Z",

"_id": "7aclSgufnQ9XHARYXhN99s",

"_rev": "T1LmJMY42FIQlwl7lqGGcK",

"_type": "gardenEvent",

"_updatedAt": "2022-12-06T21:41:17Z",

"active": false,

"due": "2023-01-24T14:46:18-05:00",

"program": {

"_ref": "rVRy5W0UdT6OoDK7hllSvP",

"_type": "reference"

},

"type": "maintenance"

},

{

"_createdAt": "2022-12-06T19:46:26Z",

"_id": "7aclSgufnQ9XHARYXhN9BD",

"_rev": "T1LmJMY42FIQlwl7lqGGXI",

"_type": "gardenEvent",

"_updatedAt": "2022-12-06T21:41:17Z",

"active": false,

"due": "2023-01-31T14:46:18-05:00",

"program": {

"_ref": "rVRy5W0UdT6OoDK7hllSvP",

"_type": "reference"

},

"type": "maintenance"

},

{

"_createdAt": "2022-12-06T19:46:26Z",

"_id": "7aclSgufnQ9XHARYXhN9Dt",

"_rev": "7aclSgufnQ9XHARYXhTDGr",

"_type": "gardenEvent",

"_updatedAt": "2022-12-06T21:41:17Z",

"active": false,

"due": "2022-12-20T14:46:18-05:00",

"program": {

"_ref": "rVRy5W0UdT6OoDK7hllSvP",

"_type": "reference"

},

"type": "complete-tower-setup"

},

{

"_createdAt": "2022-12-06T19:46:26Z",

"_id": "T1LmJMY42FIQlwl7lq4toi",

"_rev": "T1LmJMY42FIQlwl7lqGGUm",

"_type": "gardenEvent",

"_updatedAt": "2022-12-06T21:41:17Z",

"active": false,

"due": "2022-12-13T14:46:18-05:00",

"program": {

"_ref": "rVRy5W0UdT6OoDK7hllSvP",

"_type": "reference"

},

"type": "initial-tower-setup"

},

{

"_createdAt": "2022-12-06T19:46:26Z",

"_id": "T1LmJMY42FIQlwl7lq4trE",

"_rev": "T1LmJMY42FIQlwl7lqGGZo",

"_type": "gardenEvent",

"_updatedAt": "2022-12-06T21:41:17Z",

"active": false,

"due": "2022-12-06T19:46:18.074Z",

"program": {

"_ref": "rVRy5W0UdT6OoDK7hllSvP",

"_type": "reference"

},

"type": "plant-seeds"

},

{

"_createdAt": "2022-12-06T19:46:26Z",

"_id": "T1LmJMY42FIQlwl7lq4twG",

"_rev": "T1LmJMY42FIQlwl7lqGGeq",

"_type": "gardenEvent",

"_updatedAt": "2022-12-06T21:41:17Z",

"active": false,

"due": "2023-01-17T14:46:18-05:00",

"program": {

"_ref": "rVRy5W0UdT6OoDK7hllSvP",

"_type": "reference"

},

"type": "maintenance"

},

{

"_createdAt": "2022-12-06T19:46:26Z",

"_id": "T1LmJMY42FIQlwl7lq4uGO",

"_rev": "7aclSgufnQ9XHARYXhTDIC",

"_type": "gardenEvent",

"_updatedAt": "2022-12-06T21:41:17Z",

"active": false,

"due": "2023-02-07T14:46:18-05:00",

"program": {

"_ref": "rVRy5W0UdT6OoDK7hllSvP",

"_type": "reference"

},

"type": "maintenance"

},

{

"_createdAt": "2022-12-06T19:46:26Z",

"_id": "drafts.7aclSgufnQ9XHARYXhN9Dt",

"_rev": "7aclSgufnQ9XHARYXhTDMD",

"_type": "gardenEvent",

"_updatedAt": "2022-12-06T21:41:17Z",

"active": false,

"due": "2022-12-20T14:46:18-05:00",

"program": {

"_ref": "rVRy5W0UdT6OoDK7hllSvP",

"_type": "reference"

},

"type": "complete-tower-setup"

},

{

"_createdAt": "2022-12-06T19:46:26Z",

"_id": "drafts.T1LmJMY42FIQlwl7lq4toi",

"_rev": "rVRy5W0UdT6OoDK7hlv3aM",

"_type": "gardenEvent",

"_updatedAt": "2022-12-06T21:41:17Z",

"active": false,

"due": "2022-12-13T14:46:18-05:00",

"program": {

"_ref": "rVRy5W0UdT6OoDK7hllSvP",

"_type": "reference"

},

"type": "initial-tower-setup"

},

{

"_createdAt": "2022-12-06T19:46:26Z",

"_id": "drafts.T1LmJMY42FIQlwl7lq4trE",

"_rev": "rVRy5W0UdT6OoDK7hlv3gP",

"_type": "gardenEvent",

"_updatedAt": "2022-12-06T21:41:17Z",

"active": false,

"due": "2022-12-06T19:46:18.074Z",

"program": {

"_ref": "rVRy5W0UdT6OoDK7hllSvP",

"_type": "reference"

},

"type": "plant-seeds"

},

{

"_createdAt": "2022-12-06T19:46:26Z",

"_id": "drafts.T1LmJMY42FIQlwl7lq4twG",

"_rev": "T1LmJMY42FIQlwl7lqGGyy",

"_type": "gardenEvent",

"_updatedAt": "2022-12-06T21:41:17Z",

"active": false,

"due": "2023-01-17T14:46:18-05:00",

"program": {

"_ref": "rVRy5W0UdT6OoDK7hllSvP",

"_type": "reference"

},

"type": "maintenance"

},

{

"_createdAt": "2022-12-06T19:46:26Z",

"_id": "drafts.T1LmJMY42FIQlwl7lq4uGO",

"_rev": "rVRy5W0UdT6OoDK7hlv3eO",

"_type": "gardenEvent",

"_updatedAt": "2022-12-06T21:41:17Z",

"active": false,

"due": "2023-02-07T14:46:18-05:00",

"program": {

"_ref": "rVRy5W0UdT6OoDK7hllSvP",

"_type": "reference"

},

"type": "maintenance"

},

{

"_createdAt": "2022-12-06T19:46:26Z",

"_id": "drafts.rVRy5W0UdT6OoDK7hllVSf",

"_rev": "T1LmJMY42FIQlwl7lqGGtw",

"_type": "gardenEvent",

"_updatedAt": "2022-12-06T21:41:17Z",

"active": false,

"due": "2023-01-10T14:46:18-05:00",

"program": {

"_ref": "rVRy5W0UdT6OoDK7hllSvP",

"_type": "reference"

},

"type": "maintenance"

},


"_createdAt": "2022-12-06T19:46:26Z",

"_id": "rVRy5W0UdT6OoDK7hllVSf",

"_rev": "T1LmJMY42FIQlwl7lqGGwS",

"_type": "gardenEvent",

"_updatedAt": "2022-12-06T21:41:17Z",

"active": false,

"due": "2023-01-10T14:46:18-05:00",

"program": {

"_ref": "rVRy5W0UdT6OoDK7hllSvP",

"_type": "reference"

},

"type": "maintenance"

}

]
Dec 7, 2022, 12:23 PM
The “type” for each object comes back as just the value. I’d like to return both the value and title.
Dec 7, 2022, 12:24 PM
Dont think you can do that (if i remember correctly)
Dec 7, 2022, 12:32 PM
The title you're specifying in your schema does not get written to your actual data when you're using a string field. If you want to make the title and the value available, you'll need to use an object instead.
Dec 7, 2022, 5:15 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?