passing id through Link to getServerSideProps :next js

Meenu Ks

i have some alert compoents. from each of the compoent i want to pass the itm._id

and recive it in [itm].jsx in the same folder in the [itm].jsx i want to use it in the getServerSideProps funtion to fetch data

index.jsx

  <div className="question11">
            {data.map((itm) => (
              <Link
                key={itm._id}
                href={{
                  pathname: "/[itm]",
                  query: itm._id,
                }}
                as={`/${encodeURIComponent(
                  itm.Name.replace(/[^a-zA-Z0-9 - _ . ~]/g, "").replace(
                    / /g,
                    "-"
                  )
                )}`}
              >
                <Alert className="question13">{itm.Name}</Alert>
              </Link>
            ))}
          </div>

flowing is the getServerSideProps function the error now i am getting is that

Server Error FetchError: invalid json response body at https://askover.wixten.com/questone/[object%20Object] reason: Unexpected token < in JSON at position 0

i think the error is id is recived as object how do i fix this

[itm].jsx

export async function getServerSideProps(query) {
  var id1 = query;

  console.log(id1);
  const queryRequest = fetch("https://askover.wixten.com/questone/" + id1).then(
    async (res) => await res.json()
  );
  const answerRequest = fetch(
    "https://askover.wixten.com/answersapi/" + id1
  ).then(async (res) => await res.json());

  const responses = await Promise.all([queryRequest, answerRequest]);
  const [posts, answerPosts] = await Promise.all(responses);

  return {
    props: {
      posts,
      answerPosts,
    },
  };
}

enter image description here

enter image description here

enter image description here

Saral Karki

Try this: In Link Tag pass query as

<div className="question11">
            {data.map((itm) => (
              <Link
                key={itm._id}
                href={{
                  pathname: "/[itm]",
                  query: {id: itm._id},
                }}
                as={`/${encodeURIComponent(
                  itm.Name.replace(/[^a-zA-Z0-9 - _ . ~]/g, "").replace(
                    / /g,
                    "-"
                  )
                )}`}
              >
                <Alert className="question13">{itm.Name}</Alert>
              </Link>
            ))}
          </div>

And in getserversideprops you can access it like this

export async function getServerSideProps(context) {
  console.log(contex.params.id)  //If doesn't work use context.query.id
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Passing ID through router-link in Vue.js

Next js not running getServerSideProps

Passing id through an link href in Laravel

Load a component on passing id through link

How to access both the params and query string from a next.js Link in getServerSideProps?

Next JS [id] error Error serializing `.data` returned from `getServerSideProps` in "/services/[id]"

getServerSideProps in _app.tsx in Next.js

How to call getServerSideprops with useEffect in Next.js

Preloading getServerSideProps data with Next.js?

Next.js getServerSideProps always undefined

Internal API fetch with getServerSideProps? (Next.js)

Next.js getServerSideProps | Strange behavior for a function

"query is not a function" Next Js getServerSideprops and firebase error

next js console about getServerSideProps props

Passing id in json through angular js

ReferenceError: window is not defined in getServerSideProps for Next.js App And I Need to Store A Variable Persistantly in The getServerSideProps Func

Passing ID through pages

Next.js imported function errors out with 'ReferenceError' in getServerSideProps

Next.js graphql context is empty {} on SSR getServerSideProps

How to access route parameter inside getServerSideProps in Next.js?

Next.js getServerSideProps use /api locally, in PR preview, and in production

Why cookies do not come to the server via getServerSideProps next.js?

Why are cookies not sent to the server via getServerSideProps in Next.js?

How to get full browser url in next js, getServerSideProps

Access data returned by getServerSideProps inside Layout in Next.js

Next.js getServerSideProps getDocs with Firebase Firestore rules

Next.js Types not being infered from getServerSideProps to NextPage

PHP: passing a variable through a link

How to get rid of ? in the URL when passing data via link component in next.js?