#hzethraeus-products

1 messages · Page 1 of 1 (latest)

ember sluice
#

hi there, reading...

#

I don't really understand exactly

#

what is setKurs?

#

is this React? Are you describing trying to set state in a React component?

hoary shuttle
ember sluice
#

could you share the complete code of the component? It's hard to say what might be happening with just that snippet. It's not really Stripe-related but we can try to look.

hoary shuttle
#

Yes, thanks alot;

export default function Classes() {
    const [kurs, setKurs] = useState("empty");

    const getProducts = async() =>{
        
        try{
            const stripe = require('stripe')('sk_test_.........');
        
            const products = await stripe.products.list({
              limit: 1,
            });
    
            console.log(products);
            const name = await products.data[0].name;
            console.log(name);
            return (name);
        }catch(err){
            console.error(err);
        }
    }
    var prodName="";
    getProducts().then(response => {
        console.log(typeof response);
        console.log(response);
        prodName=response.toString();
        setKurs(prodName);
      });
 
    return (
    <div>
        <p>{kurs}</p>
        <p>{prodName}</p>
   </div>
    )
  }
#

kurs is showing the word "empty"
While prodName is not displaying anything

arctic cave
#

HI there 👋 apologies, but I'm not terribly familiar with Next, so I'm going to be doing some "thinking out loud" while I read through the info. Please let me know if it seems like I'm missing anything while I do.

#

So to start, you're retrieving products from Stripe (currently limiting to one result), and then pulling out the name for that product. You say the log lines during the getProducts call print what you're expecting them to.

#

You then pass that response to another function, and based on the logs there you're seeing that getProducts is returning a string containing the name of a product.

#

But when you try to pass that value to prodName or setKurs they don't seem to populate to the expected value?

hoary shuttle
#

Everything you said is correct, except that passing
prodName=response.toString();
actually populates prodName (If i console log it it looks good)

#

I am realizing now that this probably has 0 to do with stripe and a 100% to do with useState / Nextjs

#

I posted the question in a NextJs channel now.

arctic cave
#

I think you're right. Initially I was wondering if that entire function was being re-executed every time kurs was updated and was re-initializing the state, but I'm pretty sure if that were the case it would loop endlessly rather than settling on kurs being empty.