Custom discount / graphql limits

I have created a discount app that uses a resource picker to only apply the discount to selected products.

The problem is that when I select more than 4 products I get an error generated from by the Shopify function.

Error parsing selectedProducts: TypeError: cannot read property 'reduce' of undefined

It works fine with any combination of variants and in the admin interface the product list saves with more than 4 products selected so the products are being stored in the metafield there’s just a problem when the discount function retrieves them when there’s more than 4.

I’ve tried to get the discount metfields from GRaphicQL but I couldn’t work it out.

here’s my input query for the discount function.

query Input {
  cart {
    lines {
      quantity
      merchandise {
        __typename
        ...on ProductVariant {
          id
        }
      }
    }
    buyerIdentity {
      email
      customer{
        metafield(namespace: "loyalty-discount", key: "loyalty_tier") {
          value
        }
      }
    }
  }
  discountNode {
    functionConfiguration: metafield(namespace: "$app:loyalty-discount", key: "function-configuration") {
      value
    }
    selectedProducts: metafield(namespace: "$app:loyalty-discount", key: "selected-products") {
      value
    }
  }
}

Thanks Dan

Hi Dan-Hrpr,

This doesn’t seem like you’re running into a limit (e.g. instruction count limit or input query limit).

Specifically, the error message “TypeError: cannot read property ‘reduce’ of undefined” makes me think it’s a JS runtime error.

If you console.error the input when your function runs, you might be able to identify the cause. Maybe selectedProducts, or one of its properties, is undefined.

Best,

Tobe

Hi Tobe,

It is undefined but only when I save more than 4 products in it.

Cheers Dan

You might be running into the 10kb metafield size limit, then. I recommend only storing the IDs of products. I don’t know if you are storing other information about products in the metafield, but it fills up fast.

I did think that but it works in the backend in the product selector.

So if I add 20 products in the product selector and then save the discount then when I edit the discount again the 20 products are still selected.

I will try and save the variant id’s only like you suggest.