tRPCttRPC
Powered by
aminos.A
tRPC•3y ago•
2 replies
aminos.

tRPC querying data via function not working

In the below code,
fetchSocialMediaData
fetchSocialMediaData
is logging
themeId
themeId
but, not
response
response
, getting error as mentioned in the screenshot attached. Is there anything I missed from docs? Correct me wherever I am wrong.

type SocialMediaData = {
    id: string;
    createdAt: Date;
    updatedAt: Date;
    copySocialMediaType: "HASHTAGS" | "POST";
    copySocialMediaCopy: string;
    organizationId: string;
    themeId: string;
    userId: string;
  };

  const [themeIds, setThemeIds] = useState<string[]>([]);
  const [arrayToStoreSocialData, setArrayToStoreSocialData] = useState<
    SocialMediaData[]
  >([]);

  const {
    data: allCreateSocialsData,
    isLoading: isLoadingCreateSocials,
    error: errorCreateSocials,
  } = api.createSocialPost.getAllCreateSocialPosts.useQuery();

  const fetchAndSetThemeIds = () => {
    try {
      const fetchedThemeIds: string[] | undefined =
        allCreateSocialsData?.map((socialPost) => socialPost.themeId) || [];
      setThemeIds(fetchedThemeIds);
    } catch (error) {
      console.error("Error fetching theme IDs:", error);
    }
  };

  console.log("themeIds", themeIds);

  useEffect(() => {
    fetchAndSetThemeIds();
  }, [allCreateSocialsData]);

  const fetchSocialMediaData = (themeId: string) => {
    try {
      console.log("themeId", themeId);
      const response =
        api.copySocialMedia.getAllSocialMediaCopyOfTheme.useQuery({
          themeId: themeId,
        });

      console.log("response", response);

      const socialMediaData: SocialMediaData[] = response?.data || [];

      setArrayToStoreSocialData((prevData) => [
        ...prevData,
        ...socialMediaData,
      ]);
    } catch (error) {
      console.error("Error fetching social media data:", error);
    }
  };

  useEffect(() => {
    themeIds.forEach((themeId) => {
      fetchSocialMediaData(themeId);
    });
  }, [themeIds]);

  console.log("arrayToStoreSocialData", arrayToStoreSocialData);
type SocialMediaData = {
    id: string;
    createdAt: Date;
    updatedAt: Date;
    copySocialMediaType: "HASHTAGS" | "POST";
    copySocialMediaCopy: string;
    organizationId: string;
    themeId: string;
    userId: string;
  };

  const [themeIds, setThemeIds] = useState<string[]>([]);
  const [arrayToStoreSocialData, setArrayToStoreSocialData] = useState<
    SocialMediaData[]
  >([]);

  const {
    data: allCreateSocialsData,
    isLoading: isLoadingCreateSocials,
    error: errorCreateSocials,
  } = api.createSocialPost.getAllCreateSocialPosts.useQuery();

  const fetchAndSetThemeIds = () => {
    try {
      const fetchedThemeIds: string[] | undefined =
        allCreateSocialsData?.map((socialPost) => socialPost.themeId) || [];
      setThemeIds(fetchedThemeIds);
    } catch (error) {
      console.error("Error fetching theme IDs:", error);
    }
  };

  console.log("themeIds", themeIds);

  useEffect(() => {
    fetchAndSetThemeIds();
  }, [allCreateSocialsData]);

  const fetchSocialMediaData = (themeId: string) => {
    try {
      console.log("themeId", themeId);
      const response =
        api.copySocialMedia.getAllSocialMediaCopyOfTheme.useQuery({
          themeId: themeId,
        });

      console.log("response", response);

      const socialMediaData: SocialMediaData[] = response?.data || [];

      setArrayToStoreSocialData((prevData) => [
        ...prevData,
        ...socialMediaData,
      ]);
    } catch (error) {
      console.error("Error fetching social media data:", error);
    }
  };

  useEffect(() => {
    themeIds.forEach((themeId) => {
      fetchSocialMediaData(themeId);
    });
  }, [themeIds]);

  console.log("arrayToStoreSocialData", arrayToStoreSocialData);
image.png
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Querying external api in trpc router not working
josh-dev627Jjosh-dev627 / ❓-help
3y ago
TRPC Stream not working,
2035Builder22035Builder / ❓-help
2y ago
Use TRPC function inside of a TRPC function
santiSsanti / ❓-help
3y ago
Transformers not transforming data (trpc-sveltekit)
SudoerWithAnOpinionSSudoerWithAnOpinion / ❓-help
3y ago