Why do I keep getting this error? Iam using nodejs, and am not that familiar with backend programming, so I cant really know whats going on, If anyone is fimiliar with this error or knows what causing, PLEASE HELP ME🙏🙏😌
Here is my code??
async function verify() {
const ticket = await client.verifyIdToken({
idToken: token,
audience: CLIENT_ID, // Specify the CLIENT_ID of the app that accesses the backend
});
const payload = ticket.getPayload();
const userid = payload['sub'];
}
verify()
.then(()=>{
res.cookie('session-token', token);
res.send('success')
})
.catch(console.error);
})
const stripe = require('stripe')(process.env.STRIPE_PRIVATE_KEY)
app.post("/create-checkout-session", async (req,res)=>{
try{
const session = await stripe.checkout.sessions.create({
payment_method_types:['card'],
mode:'payment',
line_items: req.body.items.map(item=>{
return{
price_data:{
currency:item.currency,
product_data:{
name:item.name
},
unit_amount: item.priceInCents,
},
quantity:item.quantity
}
}),
success_url:`${process.env.SERVER_URL}/success.html`,
cancel_url:`${process.env.SERVER_URL}/cancel.html`
})
res.json({url:session.url})
}catch(e){
res.status(500).json({error:e.message})
}
res.json({url:'Hi'})
})````
you are trying to set Http headers after they are sent.
This error is occuring because your code is using res.json in both try and catch blocks. It means res.json() will be executed no matter error occurs or not. Which sends json response.
Then you are using res.json outside the try/catch blocks, it means you're trying to send response after it is already sent.
And that is causing this error.