[英]req.query in Express.js is undefined
I am implementing Stripe Checkout
in a MERN application.我正在 MERN 应用程序中实现
Stripe Checkout
。
After a customer makes a successful payment, he/she is redirected to a checkout success URL where CheckoutSuccess
page renders on the screen.客户成功付款后,他/她将被重定向到结帐成功 URL,其中
CheckoutSuccess
页面呈现在屏幕上。 Also, Stripe sends a session_id
in the URL, as shown in the picture below.此外,Stripe 在 URL 中发送
session_id
,如下图所示。
When CheckoutSuccess
page renders, I send a POST
request to /api/order/success
.当
CheckoutSuccess
页面呈现时,我向/api/order/success
发送一个POST
请求。 In the route handler function, I need to access the session-id
(which is in the URL) sent by Stripe.在路由处理程序 function 中,我需要访问 Stripe 发送的
session-id
(在 URL 中)。
The problem I am facing is when I try to console.log req.query
, I get a blank object;我面临的问题是当我尝试 console.log
req.query
时,我得到一个空白的 object; therefore req.query.session_id
returns undefined
.因此
req.query.session_id
返回undefined
。
Why is req.query
a blank object?为什么
req.query
是空白的 object?
The code snippets are as follows: orderRoutes.js代码片段如下: orderRoutes.js
const express = require("express");
const asyncHandler = require("express-async-handler");
const dotenv = require("dotenv");
dotenv.config();
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
const router = express.Router();
router.post(
"/success",
asyncHandler(async (req, res) => {
console.log(req.query);
const session = await stripe.checkout.sessions.retrieve(
req.query.session_id
);
const customer = await stripe.customers.retrieve(session.customer);
res.send(customer);
})
);
module.exports = router;
orderSlice.js orderSlice.js
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";
const initialState = {
status: "idle",
customer: null,
error: null,
};
export const fetchCustomer = createAsyncThunk(
"order/fetchCustomer",
async (_, { rejectWithValue }) => {
try {
const { data } = await axios.post("/api/order/success");
return data;
} catch (err) {
return rejectWithValue(err.response.data);
}
}
);
export const orderSlice = createSlice({
name: "order",
initialState,
reducers: {},
extraReducers: {
[fetchCustomer.pending]: (state, action) => {
state.status = "loading";
},
[fetchCustomer.fulfilled]: (state, action) => {
state.status = "succeeded";
state.customer = action.payload;
},
[fetchCustomer.rejected]: (state, action) => {
state.status = "failed";
state.error = action.payload.message;
},
},
});
export default orderSlice.reducer;
Moving out of comment and into an answer退出评论并进入答案
your fetch() call isn't passing any data or query params, right?您的 fetch() 调用没有传递任何数据或查询参数,对吗? that seems to be the issue here
这似乎是这里的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.