javascriptcontent-management-systemsanitygroq

Sanity GROQ query to not return nullish values


I was querying Sanity with GROQ and wanted to get response excluding nullish values in case there is no image url is available but if it is then return the image url.

Doing like this:

defined(image) => {image},

Wanted to get

image:"png image url here"

but getting

"image": {
  "_type": "file",
  "asset": {
    "_ref": "abc-png",
    "_type": "reference"
  }
}

Solution

  • I found the answer by myself:

    If we want to get image: "image url here" in case there is image coming from Sanity, then doing like this

    defined(image) => {"image":image.asset->url}
    

    will send you image:"image url here" but if it is not coming and value is null then it will not send the attribute at all.

    Hope this will be helpful!