Call a UID and a Predicate at the same time?

Is it possible to call two queries at the same time, even if one query is the helper getByUID with SvelteKit's load function? I am trying to call all of my doc types in my blog post which I am calling the data with the UID helper, but I need to pass all the blog-type data into another component. I don't see much documentation about calling multiple queries except with multiple predicates. Here is what i tried below and the result i received was posts: [at(document.type, "blog_post")]

<script context="module">
    import Prismic from "@prismicio/client";
    import PrismicDom from "prismic-dom";
    import Client from "../../../utils/client";
    export async function load({ page }) {
        const { uid } = page.params;
        const document = await Client.getByUID("blog_post", uid);
        const posts = await Prismic.Predicates.at("document.type", "blog_post");
        if (document) {
            return {
                props: {
                    document,
                    uid,
                    posts,
                },
            };
        } else {
            return {
                status: 404,
                error: "Post not found",
            };
        }
    }
</script>

<script>
    export let document;
    export let posts;
    console.log("posts:", posts);
    const post = document.data;
</script>

Hey @teddy_stanowski, thanks for reaching out.

At first glance, it seems like those queries should return a successful response. What are the props returning, a string?

It seems to return an object of the type of document versus all of the documents posts: [at(document.type, "blog_post")]. document itself returns the post data which i am iterating through successfully.

I am confused because I am using the same exact query in our blog index as shown below which works

<script context="module">
    import Client from "../../../utils/client";
    import Prismic from "@prismicio/client";
    export async function load() {
        const document = await Client.query(
            Prismic.Predicates.at("document.type", "blog_post")
        );
        return {
            props: {
                document,
            },
        };
    }
</script>

A colleague just pointed out to me that you are missing wrap the predicate with the client, just like you're doing in the 'blog index'. It should look like this:


<script context="module">
import Prismic from "@prismicio/client";
import PrismicDom from "prismic-dom";
import Client from "../../../utils/client";
export async function load({ page }) {
  const { uid } = page.params;
  const document = await Client.getByUID("blog_post", uid);
  const posts = await Client.query(
    Prismic.Predicates.at("document.type", "blog_post")
  );
  if (document) {
    return {
      props: {
        document,
        uid,
        posts,
      },
    };
  } else {
    return {
      status: 404,
      error: "Post not found",
    };
  }
}
</script>

<script>
export let document;
export let posts;
console.log("posts:", posts);
const post = document.data;
</script>

ahhh yes! thank you! i forgot about wrapping it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.