admin管理员组

文章数量:1266572

I am setting up a payment system with Stripe and I would like to add some metadata to the customer object. I would like to add my workspace id to the metadata property of the customer. I tried the below code, but it returns the following error:

⛔️ Error:
 Error: Invalid val: {:_bsontype=>"ObjectID", :id=>"\\HÉ\u001E��\u000F�=��"} must be a string under 500 characters

I've logged the workspace Id that I add to this metadata property, but it seems like it's just a regular mongodb ObjectId. Can anyone see what I did wrong?

The code that is supposed to add metadata to the customer I create

    // find the current User and use his workspace ID
    const user = await User.findOne({ _id: req.userId });
    const workspaceId = user._workspace;

    // get the payment plan
    const plan = await stripe.plans.retrieve('plan_EK1uRUJLJcDS6e');

    //   // then we create a new customer
    const customer = await stripe.customers.create({
      email,
      source,
      metadata: {
        workspace_id: workspaceId
      }
    });

    res.status(200).json({
      message: 'payment plete',
      subscription: adjustedSubscription
    });

I am setting up a payment system with Stripe and I would like to add some metadata to the customer object. I would like to add my workspace id to the metadata property of the customer. I tried the below code, but it returns the following error:

⛔️ Error:
 Error: Invalid val: {:_bsontype=>"ObjectID", :id=>"\\HÉ\u001E��\u000F�=��"} must be a string under 500 characters

I've logged the workspace Id that I add to this metadata property, but it seems like it's just a regular mongodb ObjectId. Can anyone see what I did wrong?

The code that is supposed to add metadata to the customer I create

    // find the current User and use his workspace ID
    const user = await User.findOne({ _id: req.userId });
    const workspaceId = user._workspace;

    // get the payment plan
    const plan = await stripe.plans.retrieve('plan_EK1uRUJLJcDS6e');

    //   // then we create a new customer
    const customer = await stripe.customers.create({
      email,
      source,
      metadata: {
        workspace_id: workspaceId
      }
    });

    res.status(200).json({
      message: 'payment plete',
      subscription: adjustedSubscription
    });
Share Improve this question asked Jan 23, 2019 at 19:55 tillytilly 2,61010 gold badges42 silver badges73 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

The values you store in metadata can only be strings of up to 500 characters. In this case you'd want to parse your workspaceId as a string. Looks like you'd want to run toString() or toHexString() on that ObjectId.

I ran into the same problem and found Paul's response. Finally works!

router.post("/create-checkout-session", async (req, res) => {
const customer = await stripe.customers.create({
metadata: {
  userId: req.body.userId.toString(),
  cart: JSON.stringify(req.body.cartItems.toString()),
},

});

本文标签: javascriptAdding metadata to a Stripe customer objectStack Overflow