return NextResponse.next() not working
Unanswered
Param san posted this in #help-forum
I am trying to get the formData from middleware for the particular router "orders". It seems to be just loading and not fetching anything as well as showing.
43 Replies
New Guinea Singing Dog
Are you sure you set up the formData() property on the request right?
Wouldn't it be easier to just have it as a seperate function
That might be where your error is coming from
@New Guinea Singing Dog Are you sure you set up the formData() property on the request right?
I am getting the data from formData not from the routes.
@New Guinea Singing Dog Wouldn't it be easier to just have it as a seperate function
How exactly? Please elaborate
New Guinea Singing Dog
Well are you querying a database and checking if orders exist?
@Param san I am getting the data from formData not from the routes.
New Guinea Singing Dog
So it always returns something?
@New Guinea Singing Dog So it always returns something?
Yea, it returns something everytime atleast for now.
New Guinea Singing Dog
Then what is the issue?
If there is no forms, you return redirect and the request moves on from the middleware to the component
Else it just reutrns with .next()
If there is no forms, you return redirect and the request moves on from the middleware to the component
Else it just reutrns with .next()
@New Guinea Singing Dog Well are you querying a database and checking if orders exist?
I want to make a new row for every successfull payment, when it returns the formData
@New Guinea Singing Dog Then what is the issue?
If there is no forms, you return redirect and the request moves on from the middleware to the component
Else it just reutrns with .next()
Yea, but is just keeps loading. Don't show anything related to the route. Just console.logs the formData
New Guinea Singing Dog
But it doesn't keep loading? Because you are awaiting it and once it has resolved, you have that conditional logic
And you've told it just to console.log() it then return redirect()
And you've told it just to console.log() it then return redirect()
You are right, it should keep loading while req is resolved. But we are just console logging here.
New Guinea Singing Dog
Nope that's normal when querying your DB and doing requests on the server side
Means it saves performances and resources bc its not on your device, but can take some time to actually fetch the data
Means it saves performances and resources bc its not on your device, but can take some time to actually fetch the data
@New Guinea Singing Dog Nope that's normal when querying your DB and doing requests on the server side
Means it saves performances and resources bc its not on your device, but can take some time to actually fetch the data
Do you know any way around or as you said before making function for it.
New Guinea Singing Dog
I'm still not really gettinig what your issue is
If you want to make it so that if orders does not exist, it redirects, you could do it on client side
If you want to make it so that if orders does not exist, it redirects, you could do it on client side
where you use useEffect()
and <Suspense>
Or could have it in a server component but it will be similar on how it awaits the data first before returning/rendering depending of the data exists or not
@New Guinea Singing Dog I'm still not really gettinig what your issue is
If you want to make it so that if orders does not exist, it redirects, you could do it on client side
My payment gateway sends me handler function as well as callback. The thing I have to do is
Make a row in DB everytime the successfull data is returnedNew Guinea Singing Dog
Wouldn't it be easiier to have this in an API
where when the user clicks "Pay", you run a function on the server that tires and process the payment?
where when the user clicks "Pay", you run a function on the server that tires and process the payment?
@New Guinea Singing Dog Wouldn't it be easiier to have this in an API
where when the user clicks "Pay", you run a function on the server that tires and process the payment?
Idk how to do that. Can you write simple steps for it and I would be able to do the rest.
If possible
New Guinea Singing Dog
So are you trying to keep a record of payed orders?
Let me make an Excalidraw how everything works
New Guinea Singing Dog
'use client'
export default function Pay() {
async function handlePayment() {
user = //get user information
await fetch('/api/makePayment', {method: "POST", body: JSON.stringify(user)})
}
return (
<button onClick={() => {handlePayment()}}>Pay here</button
)
}Like something like that instead of handling it in your middleware
const makePayment = async () => {
const orderData = await orderAPI({ totalAmount });
const orderId = orderData.id;
console.log('Amount Due: ', orderData.amount_due);
if (!orderId) return;
await supabase.from("orders").insert([
{
user_id: user,
amount: orderData.amount,
razor_orderId: orderId,
},
]);
const loadRazorpayScript = async () => {
const script = document.createElement("script");
script.src = "https://checkout.razorpay.com/v1/checkout.js";
script.async = true;
script.onload = initializeRazorpay;
document.body.appendChild(script);
return () => {
document.body.removeChild(script);
};
};
const initializeRazorpay = async () => {
const rzp1 = new window.Razorpay({
key: "rzp_test_G89RkM1qXUS2OF",
amount: orderData.amount * 100,
currency: "INR",
order_id: orderId,
prefill: {
name: "",
email: "",
contact: "",
},
callback_url: "http://localhost:3000/orders",
});
rzp1.open();
};
loadRazorpayScript();
};This is the function that handles payment.
New Guinea Singing Dog
Ok sweet and where is your formData() function on the request coming from?
@New Guinea Singing Dog Ok sweet and where is your formData() function on the request coming from?
You can the see the callback, it is sending the response to
/orders.So I am trying to capture that response in middleware for
/orders route and would send that to the DBNew Guinea Singing Dog
I think it will be easier if you just try run the function instead of doing it
if success? Insert into DB
If not? Tell the user their payment failed
.onload, and then you could have error handlingif success? Insert into DB
If not? Tell the user their payment failed
Do you have any structure for that function, This is my first time handling functions, 🥲
New Guinea Singing Dog
import logo from './logo.svg';
import './App.css';
function App() {
async function displayRazorpay () {
const res = await loadScript('https://checkout.razorpay.com/v1/checkout.js')
if (!res){
alert('Razropay failed to load!!')
return
}
const data = await fetch('http://localhost:1769/razorpay', {method: 'POST'}).then((t) =>
t.json()
)
console.log(data)
const options = {
"key": "YOUR_KEY_ID", // Enter the Key ID generated from the Dashboard
"amount": "50000", // Amount is in currency subunits. Default currency is INR. Hence, 50000 refers to 50000 paise
"currency": "INR",
"name": "Acme Corp",
"description": "Test Transaction",
"image": "https://example.com/your_logo",
"order_id": "order_IluGWxBm9U8zJ8", //This is a sample Order ID. Pass the `id` obtained in the response of Step 1
"callback_url":"http://localhost:1769/verify",
"notes": {
"address": "Razorpay Corporate Office"
},
"theme": {
"color": "#3399cc"
}
};
const paymentObject = new window.Razorpay(options);
paymentObject.open();
}
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<button
onClick={displayRazorpay}
>
Pay now
</button>
</header>
</div>
);
}
export default App;I was looking through the docs and found this
You see how they have it as a function on the front end and immediately called .open()
Like what i was saying earlier with a button
Ohh I see what you were trying to say earlier.
Thanks, I'll get back to you if I get by any issue.
New Guinea Singing Dog
Yeah this might be a bit easier then trying to then redirect with a new .onload function
Yeah seems to me too