我正在尝试使用以下 URL http://localhost:3000/api/hello?type=customer 访问我的 http 请求中的查询,有谁知道如何检索 type 的值?
export async function GET(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'GET') {
res.setHeader('Allow', ['GET']);
return res.status(405).end('Method Not Allowed');
}
const cusID = "**************";
const pmID = "******************";
console.log(req.query) // undefined
const type = req.query.type;
try {
if (type === 'paymentMethod') {
const paymentMethod = await stripe.customers.retrievePaymentMethod(cusID, pmID);
console.log(paymentMethod);
return res.status(200).json(paymentMethod);
} else if (type === 'customer') {
const customer = await stripe.customers.retrieve(cusID);
console.log('Customer:', customer);
return res.status(200).json(customer);
} else {
return res.status(400).json({ error: "No valid query parameter provided" });
}
} catch (error) {
console.error(error);
return res.status(500).json({ error: "Internal Server Error" });
}
}
问题:
我正在尝试使用以下网址访问我的 http 请求中的查询
可能的原因:
可能的解决方案:
使用
nextUrl
访问查询(类型):
const type = req.nextUrl.searchParams.get('type')
请阅读:
如果您有任何疑问,请发表评论(如果有必要我会更新答案)