Hi!
I’m attempting to use the Stripe API. I want to just extract the product name for the JSON object I get back.
I can print to console and I get the following JSON object:
Here’s the code on the page:
import React from 'react'
export default class Products extends React.Component {
state = {
content: 'loading...'
}
async componentDidMount() {
if (typeof fetch == 'undefined') return
const response = await fetch('/.netlify/functions/products')
const content = await response.json()
console.dir({ content })
this.setState({ content: JSON.stringify(content) })
}
render() {
const { content.products } = this.state
return (
<div style={{ color: 'white' }}>
<h2>Data received from <code>/.netlify/functions/products/</code>:</h2>
<p>content.products.data[0].metadata.name</p>
<br />
<br />
<br />
</div>
)
}
}
To me, it looks like the following code should access the product name:
content.products.data[0].metadata.name
or
content.products.data[0].name
I am trying to just get the product name. Instead, I get an output of the entire object:
{"products":{"object":"list","data":[{"id":"prod_EzVExr5aLjvMtV","object":"product","active":true,"attributes":["name"],"caption":null,"created":1556770137,"deactivate_on":[],"description":null,"images":[],"livemode":true,"metadata":{},"name":"Precise Parasite Home Test Hit","package_dimensions":null,"shippable":true,"skus":{"object":"list","data":[{"id":"sku_EzVERiWpvoz0or","object":"sku","active":true,"attributes":{"name":"Precise Parasite Home Test Hit"},"created":1556770137,"currency":"usd","image":"https://files.stripe.com/links/fl_live_MBnxbXpVarp0VElEaPUrwlBz","inventory":{"quantity":null,"type":"infinite","value":null},"livemode":true,"metadata":{},"package_dimensions":null,"price":19500,"product":"prod_EzVExr5aLjvMtV","updated":1556770137}],"has_more":false,"total_count":1,"url":"/v1/skus?product=prod_EzVExr5aLjvMtV&active=true"},"type":"good","updated":1556771045,"url":null}],"has_more":false,"url":"/v1/products"}}
But I just want to output the product name.
What am I doing wrong?
Thank you!