TypeError: res.redirect and res.status is not a function
Unanswered
English Angora posted this in #help-forum
English AngoraOP
Dear community,
Trying to implement Stripe, it looks like it's going okay overall, but it's returning this TypeError in the console.
I would love to if anyone else had this similar problem with the implementation of Stripe or in general.
All the help is welcome. Please let me know if you need any more source code.
Next.js 13.4.4 & TypeScript 5.0.4
Best regards.
Paddy
Trying to implement Stripe, it looks like it's going okay overall, but it's returning this TypeError in the console.
if (items) {
res.status(200).json({
url: session.url,
});
} else {
res.redirect(301, session.url ?? '');
}
} catch (e: any) {
console.log(e);
res.status(e.statusCode || 500).json({
message: e.message,
});
}I would love to if anyone else had this similar problem with the implementation of Stripe or in general.
All the help is welcome. Please let me know if you need any more source code.
Next.js 13.4.4 & TypeScript 5.0.4
Best regards.
Paddy
7 Replies
@English Angora Dear community,
Trying to implement Stripe, it looks like it's going okay overall, but it's returning this TypeError in the console.
if (items) {
res.status(200).json({
url: session.url,
});
} else {
res.redirect(301, session.url ?? '');
}
} catch (e: any) {
console.log(e);
res.status(e.statusCode || 500).json({
message: e.message,
});
}
I would love to if anyone else had this similar problem with the implementation of Stripe or in general.
All the help is welcome. Please let me know if you need any more source code.
Next.js 13.4.4 & TypeScript 5.0.4
Best regards.
Paddy
New Zealand Heading Dog
I had this same issue before. If you are using the App directory, I have found that you have to do responses along the lines of:
That might work, however I know you are using TypeScript and that you have to give the 'res' object a type of 'NextResponse'. I wasn't using TypeScript when I ran into this same error so I had to return what's provided in the code block above.
Hope this helps.
return NextResponse.json({ status: (statuscode), message: (message) })That might work, however I know you are using TypeScript and that you have to give the 'res' object a type of 'NextResponse'. I wasn't using TypeScript when I ran into this same error so I had to return what's provided in the code block above.
Hope this helps.
English AngoraOP
Thank you @New Zealand Heading Dog . Really appreciate you for telling me this, I will look into this. 🙂
@English Angora Dear community,
Trying to implement Stripe, it looks like it's going okay overall, but it's returning this TypeError in the console.
if (items) {
res.status(200).json({
url: session.url,
});
} else {
res.redirect(301, session.url ?? '');
}
} catch (e: any) {
console.log(e);
res.status(e.statusCode || 500).json({
message: e.message,
});
}
I would love to if anyone else had this similar problem with the implementation of Stripe or in general.
All the help is welcome. Please let me know if you need any more source code.
Next.js 13.4.4 & TypeScript 5.0.4
Best regards.
Paddy
the reason is that in app router route handlers you don't have
res. in fact you don't have the express req either, you have the native req (Request) object instead. basically the syntaxes are completely different. see https://nextjs.org/docs/app/building-your-application/routing/router-handlersEnglish AngoraOP
hmm, very interesting, thanks @joulev You both are heros, have been struggling with this for a bit and the morning sun is already up.. :p
English AngoraOP
But I think I am using this correctly, I am sharing the whole file under here, so you understand how the
@joulev
req and res are being setup.import type { NextApiRequest, NextApiResponse } from 'next';
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY ?? '', {
apiVersion: '2022-11-15',
});
console.log('outside the const handler!');
export async function POST(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'POST') {
return res.status(405).end();
}
try {
const { price, quantity, items } = req.body;
const lineItems = items
? items.map((item: any) => ({
price: item.id,
quantity: item.quantity,
}))
: [
{
price,
quantity,
},
];
console.log('logging the req.headers.origin:');
console.log(`${req.headers.origin}`);
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [
{
price: 'price_1N2UwFHjh6rwfnNSgLo5XwJs',
quantity: 1,
},
],
// line_items: lineItems,
mode: 'payment',
success_url: `http://localhost:3000/result?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `http://localhost:3000/cancel`,
automatic_tax: { enabled: true },
});
if (items) {
res.status(200).json({
url: session.url,
});
} else {
res.redirect(301, session.url ?? '');
}
} catch (e: any) {
console.log(e);
res.status(e.statusCode || 500).json({
message: e.message,
});
}
}@joulev
English AngoraOP
Thank you!