Next.js Discord

Discord Forum

Only first searchParam is defined

Answered
Molossus of Epirus posted this in #help-forum
Open in Discord
Molossus of EpirusOP
Issue where only the first url search param is defined but the others are null. It is always the first one that is defined, regardless of which one I put there. The API route gives back a url that first redirects to an app, then when an action happens in that app I am redirected back to the callbackUrl:

export async function POST(req) {
    const body = await req.json()
    const paymentRequest = await createPaymentRequest()
    if (body.mobileUser) {
        const callbackUrl = `https://mydomain.com/receipt?id=${encodeURIComponent(paymentRequest.id)}&forening=${encodeURIComponent(body.foreningName)}&alias=${encodeURIComponent(body.alias)}&ticket=${encodeURIComponent(body.ticketValue)}`
        console.log(callbackUrl)
        const appUrl = `app://paymentrequest?token=${paymentRequest.token}&callbackurl=${callbackUrl}`;
        return NextResponse.json({ url: appUrl, statusUrl: paymentRequest.location })
    } else {
        const data = await getQrCodeFromToken(paymentRequest.token)
        return NextResponse.json({data: data, statusUrl: paymentRequest.location})
    }

I have verified that the response from this API has the correct value for "callbackUrl".
The page that I am routing to looks like this:

"use client"

function Receipt() {

  const searchParams = useSearchParams();
  const id = searchParams.get("id");
  const forening = searchParams.get("forening");
  const ticket = searchParams.get("ticket");
  const alias = searchParams.get("alias");
  
  // Only param "id" is defined here as it is specified as the first param, others are null
  ...

  return ...
}


Any ideas on what I might be doing wrong? I have tried not using encodeURIComponent without any success.
Answered by DirtyCajunRice | AppDir
const searchParams = new URLSearchParams();
searchParams.set(id, paymentrequest.id)
View full answer

43 Replies

@Molossus of Epirus yeah i wouldnt do it like this at all
const searchParams = new URLSearchParams();
searchParams.set(id, paymentrequest.id)
Answer
etc
then searchParams.toString()
Molossus of EpirusOP
Not sure what you mean? Are you constructing the callbackUrl like that?
@Molossus of Epirus Not sure what you mean? Are you constructing the callbackUrl like that?
yes. You are manually doing work that is already handled for you and something is going wrong by doing so
Molossus of EpirusOP
So i would basically do that, then append searchParams.toString() after receipt in the callbackUrl?
i would also console.log window.location
in the receipt
to see what it is showing
Molossus of EpirusOP
Its not defined there though right?
window that is
you have use client
it doesnt exist on prerender but does on hydration
so you check if it exists then print
Molossus of EpirusOP
Oh yeah
should show in your browser console log
Molossus of EpirusOP
"So i would basically do that, then append searchParams.toString() after receipt in the callbackUrl?"
yeah hostandpath?${searchParams.toString()}
Molossus of EpirusOP
Alright I'll try this out
when possible, dont reinvent the wheel. You have access to the classes so you use them! gaha
Molossus of EpirusOP
Alright, did not help. Logged window.location and it is not correct, only shows the id parameter present in the url as it is the first one...
Something seems to get lost when going from the browser to the app and then back to the callbackUrl. Logging "appUrl" in the client gives the correct URL, but when the callback has happened and we are at the Receipt page the URL is incorrect 🤔
@DirtyCajunRice | AppDir show me the revised code
Molossus of EpirusOP
Sorry, I meant that it did not solve my issue, it still remains. I will show you the revised code one sec
export async function POST(req) {
    const body = await req.json()
    const paymentRequest = await createPaymentRequest()
    if (body.mobileUser) {
        const searchParams = new URLSearchParams();
        searchParams.set('id', paymentRequest.id)
        searchParams.set('forening', body.foreningName)
        searchParams.set('alias', body.alias)
        searchParams.set('ticket', body.ticketValue)
        const callbackUrl = `https://mydomain.com/receipt?${searchParams.toString()}`
        const appUrl = `app://paymentrequest?token=${paymentRequest.token}&callbackurl=${callbackUrl}`;
        return NextResponse.json({ url: appUrl, statusUrl: paymentRequest.location })
    } else {
        const data = await getQrCodeFromToken(paymentRequest.token)
        return NextResponse.json({data: data, statusUrl: paymentRequest.location})
    }

}
you are missing a step
you need another searchParams variable
and to use it for the callbackurl
that will ensure the callbackurl is encoded properly
appurl = app://paymentrequest?${outerSearchParams.toString()}
makes sense?
Molossus of EpirusOP
so i create another searchParams object (outerSearchParams) and add the callbackUrl to that variable?
with set
callbackurl and token
since both are params
Molossus of EpirusOP
Oh yeah
Ill try it thanks
np
if that doesnt work i am out of ideas because it should at that point
Molossus of EpirusOP
Thank you for your help! This finally solved it 🙂