I’m new to javascript and I’m working on a project where I need to access the prices property within a JavaScript object. This object is the product object from the stripe integration. When I log productData.prices, it shows undefined even though the productData object has the prices property. I’m not sure why this is happening. Can someone help me understand what might be causing this issue?
Here is the javascript code
export default function Subscription() {
const [loading, setLoading] = useState(false);
const [products, setProducts] = useState([]);
const { currentUser } = useAuth();
const [stripe, setStripe] = useState(null);
useEffect(() => {
const initializeStripe = async () => {
const stripe = await loadStripe(
process.env.REACT_APP_STRIPE_PUBLISHABLE_KEY
);
setStripe(stripe);
};
initializeStripe();
const q = query(collection(db, "products"), where("active", "==", true));
getDocs(q).then((querySnapshot) => {
const products = {};
querySnapshot.forEach(async (doc) => {
products[doc.id] = doc.data();
const priceSnapshot = await getDocs(
collection(db, "products", doc.id, "prices")
);
priceSnapshot.forEach((price) => {
products[doc.id].prices = {
priceId: price.id,
priceData: price.data(),
};
});
});
setProducts(products);
});
}, []);
async function loadCheckOut(priceId) {
setLoading(true);
const usersRef = doc(collection(db, "users"), currentUser.uid);
const checkoutSessionRef = collection(usersRef, "checkout_sessions");
const docRef = await addDoc(checkoutSessionRef, {
price: priceId,
trial_from_plan: false,
success_url: window.location.origin,
cancel_url: window.location.origin,
});
onSnapshot(docRef, (snap) => {
const { error, sessionId } = snap.data();
if (error) {
alert(`An error occurred: ${error.message}`);
}
if (sessionId && stripe) {
stripe.redirectToCheckout({ sessionId });
}
});
}
return (
<>
<Container className="mt-4 mb-4">
<h1 className="text-center mt-4">Choose Your Plan</h1>
<Row className="justify-content-center mt-4">
{Object.entries(products).map(([productId, productData]) => {
console.log(productData);
console.log(productData.prices);
return (
<Col md={4} key={productId}>
<Card>
<Card.Header className="text-center">
<h5>{productData.name}</h5>
<h5>$20.00 / month</h5>
</Card.Header>
<Card.Body>
<h6>{productData.description}</h6>
<Button
onClick={() => loadCheckOut(productData?.prices?.priceId)}
variant="primary"
block
disabled={loading}
>
{loading ? (
<>
<Spinner
animation="border"
size="sm"
className="mr-2"
/>
Loading...
</>
) : (
"Subscribe"
)}
</Button>
</Card.Body>
</Card>
</Col>
);
})}
</Row>
</Container>
</>
);
}
The console.log(productData);
logs the following object
{
"images": [],
"description": "Access to dashboard",
"tax_code": null,
"active": true,
"role": "premium",
"name": "Premium",
"metadata": {
"firebaseRole": "premium"
},
"prices": {
"priceId": "price_1N7SdbJHqW6OBlJ5itJgsGON",
"priceData": {
"billing_scheme": "per_unit",
"interval": "month",
"unit_amount": 2000,
"currency": "usd",
"product": "prod_NtF7pDBsqj5psh",
"transform_quantity": null,
"type": "recurring",
"active": true,
"metadata": {},
"tax_behavior": "unspecified",
"tiers": null,
"tiers_mode": null,
"interval_count": 1,
"recurring": {
"interval": "month",
"aggregate_usage": null,
"usage_type": "licensed",
"interval_count": 1,
"trial_period_days": null
},
"description": null,
"trial_period_days": null
}
}
}
The console.log(productData.prices);
logs undefined. Can someone help me understand what’s happening here and how i can resolve it? Thank you.