How to check if document has certain slice

Hi,

I have the following prismic model for ads document;

{
    "Main" : {
      "name" : {
        "type" : "Text",
        "config" : {
          "label" : "name"
        }
      },
      "url" : {
        "type" : "Text",
        "config" : {
          "label" : "url",
          "placeholder" : "Promotion's url"
        }
      },
      "images" : {
        "type" : "Image",
        "config" : {
          "constraint" : {
            "width" : 328,
            "height" : 328
          },
          "thumbnails" : [ {
            "name" : "portrait",
            "width" : 328,
            "height" : 656
          }, {
            "name" : "landscape",
            "width" : 656,
            "height" : 328
          } ],
          "label" : "images"
        }
      },
      "body" : {
        "type" : "Slices",
        "fieldset" : "Slice zone",
        "config" : {
          "labels" : null,
          "choices" : {
            "adsimagesquare" : {
              "type" : "Slice",
              "fieldset" : "AdsImageSquare",
              "description" : "Ads Image Square 1:1",
              "icon" : "image",
              "display" : "list",
              "repeat" : { },
              "non-repeat" : {
                "image" : {
                  "type" : "Image",
                  "config" : {
                    "constraint" : {
                      "width" : 328,
                      "height" : 328
                    },
                    "thumbnails" : [ {
                      "name" : "square",
                      "width" : 328,
                      "height" : 328
                    } ]
                  }
                }
              }
            },
            "adsimageportrait" : {
              "type" : "Slice",
              "fieldset" : "AdsImagePortrait",
              "description" : "Ads Image Portrait 1:2",
              "icon" : "image",
              "display" : "list",
              "repeat" : { },
              "non-repeat" : {
                "image" : {
                  "type" : "Image",
                  "config" : {
                    "constraint" : {
                      "width" : 328,
                      "height" : 656
                    },
                    "thumbnails" : [ {
                      "name" : "square",
                      "width" : 328,
                      "height" : 656
                    } ]
                  }
                }
              }
            },
            "adsimagelandscape" : {
              "type" : "Slice",
              "fieldset" : "AdsImageLandscape",
              "description" : "Ads Image Landscape 2:1",
              "icon" : "image",
              "display" : "list",
              "repeat" : { },
              "non-repeat" : {
                "image" : {
                  "type" : "Image",
                  "config" : {
                    "constraint" : {
                      "width" : 656,
                      "height" : 328
                    },
                    "thumbnails" : [ {
                      "name" : "landscape",
                      "width" : 656,
                      "height" : 328
                    } ]
                  }
                }
              }
            }
          }
        }
      }
    }
  }

And currently, I would need to grab all ads documents, that has adsimagesquare in its body.
This is my current attempt at the moment;


  useEffect(() => {
    const queries = [
      Prismic.predicates.at("document.type", "ads"),
      Prismic.predicates.has("my.ads.adsimagesquare")
    ];
    const options = {
      lang: "en-us",
      fetchLinks: [
      ],
    };

    prismicClientHttp
      .query(queries, options)
      .then((doc: any) => {
        return doc;
      })
      .catch((err) => {
        console.error(err);
        return null;
      });
  }, []);

However, I am not having any luck here. I also am failing to find the related documents or QnAs regarding the same scenario

Please kindly help, any suggestion is appreciated.

Hello @root

Welcome to the Prismic community forum and thanks for posting to us.

For the moment, has predicates works for all content types except Groups and Slices.

For retrieving all documents where a specific slice is added, you can use a key-value pair where you can add a common and unique identifier as a value like slice-identifier-adsimagesquare and you need to keep in all of the documents where the slice adsimagesquare is placed. Now, you can use fulltext for retrieving all documents where this text is available.
For eg, you can add a Select field in your adsimagesquare slices, give value in Select field like slice-identifier-adsimagesquare. Now you have to use fulltext predicates to search these slices based on this value:
[fulltext(document,"slice-identifier-adsimagesquare")]

Please find more details in the fulltext predicates.

I hope it solves your issue and let me know if you have any further questions related to it.

Thanks,
Priyanka