【问题标题】:How to get the query params inside getServerSideProps in nextjs如何在 nextjs 中的 getServerSideProps 中获取查询参数
【发布时间】:2022-01-26 13:21:04
【问题描述】:

我的网址是这样的:

http://localhost:3000/success?merchant=xxxxxx&order_id=xxxxx&payment_ref_id=xxxxxxx&status=Aborted&status_code=9999&message=Not%20a%20Nagad%20account

我需要从 getServerSideProps 中的 URL 获取 payment_ref_id。我怎样才能做到这一点? 我尝试了context.query.payment_ref_id 之类的方法,但没有奏效。 我的代码:

export const getServerSideProps: GetServerSideProps = async (context) => {
    try {
        const payment_reference_id = context.query.payment_ref_id;
        const response = await axios.get(`${BASE_URL}/remote-payment-gateway-1.0/api/dfs/verify/payment/${payment_reference_id}`,
            {
                headers: {
                    "X-KM-IP-V4": IP_ADDRESS,
                    "X-KM-Client-Type": "PC_WEB",
                    "X-KM-Api-Version": "v-0.2.0",
                    "Content-Type": "application/json",
                }
            });

        const payment_verify_res = await response.data;
        const paymentStatus = await payment_verify_res.status;

        return {
            props: {
                StatusProps: payment_verify_res || null,
                PaymentStatus: paymentStatus || null,

            },


        }

    } catch (error) {
        console.log(error);
        return { props: {} }
    }

}

【问题讨论】:

  • 如果你在访问你提供的URL时登录context.query.payment_ref_id,你会在终端看到什么?
  • @juliomalves 未定义
  • 其他查询参数undefined也是吗? context.query 返回什么?
  • const { payment_ref_id } = context.query;解构解决了我的问题

标签: reactjs next.js


【解决方案1】:

这听起来可能很愚蠢,但请检查您是否在 /pages/success.tsx/pages/success/index.tsx 中有此代码
同样在得到响应后,您没有调用 data() 方法。 您的代码应该看起来像这样:

const payment_verify_res = await response.data();
const paymentStatus = payment_verify_res.status;

【讨论】:

  • 是的,我在成功页面内
猜你喜欢
  • 2021-12-31
  • 2020-07-25
  • 2021-09-04
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
  • 2021-04-22
  • 2022-01-13
  • 1970-01-01
相关资源
最近更新 更多