Next.js Discord

Discord Forum

TypeError: res.redirect and res.status is not a function

Unanswered
English Angora posted this in #help-forum
Open in Discord
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.

    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:
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 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 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 Angora But I think I am using this correctly, I am sharing the whole file under here, so you understand how the ``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, }); } } <@484037068239142956>
No you arent. It is not req: NextRequest, res: NextResponse. Check the documentation again. You don’t have res passed as a function param
English AngoraOP
Thank you!