rbi-learning / Today-I-Learned

1 stars 0 forks source link

Today I Learned 8/17 #147

Open amatheus000 opened 4 years ago

amatheus000 commented 4 years ago

Morning Exercise and Work

Today I reenforced the concept of awaiting a function. Every time we receive data from a server we need save it in a constant that awaits the response. Also, the function needs to be set to async. This is the correct coding structure:

async function fetchProducts() { const response = await fetch('/products'); products = await response.json();}

We started working with Stripe. In it we can link post products and their prices on line. After posting several products online we will get this data for a user to see them. We used the following code to get the product name from the Stripe API and then use it for our project.

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY); app.get('/products', async (_request, response) => { const products = await stripe.products.list({ limit: 100, });

We used the same structure to get the rest of the products information (price).

// Ask stripe for all of the prices const prices = await stripe.prices.list({ limit: 100, });

Nevertheless, we needed to associate each of the products with their prices. We did this following this structure:

// Associate the products with the prices prices.data.forEach(price => {

// find the product associated with this price. const theAssociatedProduct = products.data.find( product => product.id === price.product ); // associate the product with the price theAssociatedProduct.price = price; });