I am building out a simple web app (using React, Express, and Stripe’s API). When trying to go to the external Stripe Payments page, I get a Failed to Fetch error. I’m not sure what’s going wrong, as everything besides this seems to be working just fine.
Uncaught runtime errors:
×
ERROR
Failed to fetch
TypeError: Failed to fetch
Here is my Navbar.js page:
import {Button, Container, Navbar, Modal} from 'react-bootstrap';
import { useState, useContext } from 'react';
import { CartContext } from "../CartContext";
import CartProduct from './CartProduct';
function NavbarComponent() {
const cart = useContext(CartContext);
const [show, setShow] = useState(false); // so ShoppingCart is not displaying on startup
const handleClose = () => setShow(false);
const handleShow = () => setShow(true);
const checkout = async () => {
await fetch('http://localhost:4000/checkout', {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({items: cart.items})
}).then((response) => {
return response.json();
}).then((response) => {
if(response.url) {
window.location.assign(response.url); // Forwarding user to Stripe
}
});
}
const productsCount = cart.items.reduce((sum, product) => sum + product.quantity, 0);
return (
<>
<Navbar expand="sm">
<Navbar.Brand href="/">Cozy Threads</Navbar.Brand>
<Navbar.Toggle />
<Navbar.Collapse className="justify-content-end">
<Button onClick={handleShow}>Cart ({productsCount} Items)</Button>
</Navbar.Collapse>
</Navbar>
<Modal show={show} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>Shopping Cart</Modal.Title>
</Modal.Header>
<Modal.Body>
{productsCount > 0 ?
<>
<p>Items in your cart:</p>
{cart.items.map( (currentProduct, idx) => (
<CartProduct key={idx} id={currentProduct.id} quantity={currentProduct.quantity}></CartProduct>
))}
<h1>Total: {cart.getTotalCost().toFixed(2)}</h1>
<Button variant="success" onClick={checkout}>
Purchase items!
</Button>
</>
:
<h1>There are no items in your cart!</h1>
}
</Modal.Body>
</Modal>
</>
)
}
export default NavbarComponent;
Does this have something to do with my localhosts/connections? Like I mentioned, everything seems to be working in terms of the front-end/UI just fine.