I am having a problem getting the router.query to pull back the params of the url.
I have tried several different ways of approaching this problem and think that i am using the right solution. However, I keep getting an undefined result.
Here is the query param highlighted in yellow...
/pages/artist/[artistId].js
import React, { useEffect, useState } from "react";
import { useSession } from "next-auth/react";
import { useRouter } from "next/router";
import { getDoc, doc } from "firebase/firestore";
import { db } from "../api/auth/firebase/config";
import Head from "next/head";
import ArtistHeader from "../../components/ArtistHeader";
import UploadButton from "../../components/UploadButton";
import styles from "../../styles/artistPage.module.css";
export default function Artist() {
const { data: session, status, loading } = useSession();
const [artist, setArtist] = useState();
const router = useRouter();
const { artistId } = router.query;
useEffect(() => {
if (status === "unauthenticated") {
router.push("/auth/signin");
}
}, [status, loading, router]);
// useEffect to fetch artist page data
useEffect(() => {
const fetchArtist = async () => {
const artistRef = doc(db, "users", `${artistId}`);
const docSnap = await getDoc(artistRef);
setArtist(docSnap);
};
fetchArtist();
}, [setArtist, artist, artistId]);
return (
<section className={styles.wrapper}>
<Head>
<title>{artist.screenName}</title>
</Head>
<div className={styles.artistPage}>
<ArtistHeader artist={artist} />
<div className={styles.songContainer}>
<UploadButton />
</div>
</div>
</section>
);
}
Is this problem being caused by the router not being ready and the page is trying to render before it is able to get the params?
Bit lost here and could use some pointers. Thanks in advance.