I'm trying to create a product in Stripe (using Next.js + Node) with a metadata of the following format:
metadata: {categories: ['foo', 'bar']}
but it seems I can't workaround string-only values in metadata.
Creating a product in the stripe dashboard with brackets in the metadata value result in, when being retrieved:
categories: "['foo', 'bar']"
Trying to create with Node:
const new_product = await stripe.products.create({
name: 'new product',
metadata: {categories: ['foo', 'bar']}
})
resulted in:
Error: Invalid value type: {:"0"=>"foo", :"1"=>"bar"} must be a string
This confirms that the value must be a string. The docs were kind of unclear as to the specific type allowance:
You can specify up to 50 keys, with key names up to 40 characters long and values up to 500 characters long.
But I suppose I'm stuck with string values in the metadata.
What is the best workaround for this?
I know I can loop through an object as long as its keys are indices, but I also will want more metadata keys and values. String delimination using split just sounds like a pain. I'm certain there are more ways to effectively use JS objects, but I'm trying to go as simple as possible, as non-developers will also want to make products that follow this archetype.
Thank you for any help.
Stripe stores this data as a single string and AFAIK there is no way around that.
However, when using a Stripe library like the one for Node.js, it does the conversion of the string into an object you can traverse. I created a product with the same categories as the code you shared and was able to access the .categories property using dot notation.
const getProduct = async (id) => {
console.log("Fetching product to evaluate metadata");
const product = await stripe.products.retrieve(id)
const meta = product.metadata
// console.log(meta);
console.log(meta.categories);
}
node test.js
Fetching product to evaluate metadata
["foo", "bar"]