Can't update document with object from Stripe

Hi,
I have an existing database that shows users from a website. When the user subscribes, the subscriptions object from Stripe should be added to the database. I’ve been updating the stripe checkout and know need to append the object to the user document each time there’s a purchase. I’ve been using the UpdateOne to no result. Nothing happens. Can someone help me please? I’m new to nodejs and mongodb so maybe there’s something I’m missing.
Here is my code:

       //create subscription
        const subscription = await stripe.subscriptions.create({
            customer: customer,
            items: [{ plan: 'plan_DznNb3tPEEI0cj' }],
            default_payment_method: paymentMethod,
            expand: ['latest_invoice.payment_intent']
        });

        let updateUser = await User.findOne({_id: userId });
        
        try {
            User.updateOne(
                {id: ObjectId(userId) },
                {$set: { "stripeCustomer": subscription}},
                { upsert: true }
            );
        } catch (err) {
            console.log(err);
        }

Try using await User.updateOne(...). Also, are you sure you want the statement let updateUser = await User.findOne(...in your code (what is its purpose)?

1 Like

Thank you! The await solved it! :grin:

The purpose of the let updateUser was simply to log it after and see the result, I already deleted it!

1 Like

Great! Further refer about the asynchronous JavaScript and using Promises and Callbacks with the driver APIs.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.