{"record":{"id":"9b49c3b037c6a304","repo":"medusajs/medusa","slug":"contextkey-should-be-present-as-an-array-in-t","errorCode":null,"errorMessage":"\"${contextKey}\" should be present as an array in the context for computeActions","messagePattern":"\"(.+?)\" should be present as an array in the context for computeActions","errorType":"validation","errorClass":"MedusaError","httpStatus":400,"severity":"error","filePath":"packages/modules/promotion/src/utils/compute-actions/line-items.ts","lineNumber":26,"sourceCode":"  ApplicationMethodAllocation,\n  ApplicationMethodTargetType,\n  calculateAdjustmentAmountFromPromotion,\n  ComputedActions,\n  MathBN,\n  MedusaError,\n  ApplicationMethodTargetType as TargetType,\n} from \"@medusajs/framework/utils\"\nimport { Promotion } from \"@models\"\nimport { areRulesValidForContext } from \"../validations\"\nimport { sortLineItemByPriceAscending } from \"./sort-by-price\"\nimport { computeActionForBudgetExceeded } from \"./usage\"\n\nfunction validateContext(\n  contextKey: string,\n  context: PromotionTypes.ComputeActionContext[TargetType]\n) {\n  if (!context) {\n    throw new MedusaError(\n      MedusaError.Types.INVALID_DATA,\n      `\"${contextKey}\" should be present as an array in the context for computeActions`\n    )\n  }\n}\n\nexport function getComputedActionsForItems(\n  promotion: PromotionTypes.PromotionDTO | InferEntityType<typeof Promotion>,\n  items: PromotionTypes.ComputeActionContext[TargetType.ITEMS],\n  appliedPromotionsMap: Map<string, number>,\n  allocationOverride?: ApplicationMethodAllocationValues\n): PromotionTypes.ComputeActions[] {\n  validateContext(\"items\", items)\n\n  return applyPromotionToItems(\n    promotion,\n    items,\n    appliedPromotionsMap,","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/medusajs/medusa/blob/5e06e544a296b9033f20f71f11c559f81a0e5739/packages/modules/promotion/src/utils/compute-actions/line-items.ts#L8-L44","documentation":"getComputedActionsForItems validates that the context bucket for the given contextKey (e.g., 'items', 'shipping_methods', or custom target types) exists before computing item-level adjustments. A null/undefined context for that key throws INVALID_DATA.","triggerScenarios":"Calling computeActions with a context object where the target-type key being computed is absent — e.g., a promotion with target_type 'items' but the context only contains shipping_methods, or the key was set to undefined via destructuring.","commonSituations":"Conditionally building context and skipping empty arrays (setting undefined instead of []), target_type misconfigured on the promotion, generic computeActions wrappers that forward partial contexts.","solutions":["Always include the relevant key with at least an empty array: { items: [], shipping_methods: [] }","Align the promotion's application_method.target_type with the context keys you actually supply","Map cart items/shipping methods explicitly even when the cart is empty"],"exampleFix":"// before\nconst context = {}\nif (cart.items.length) context.items = mapItems(cart.items)\nawait promotionModule.computeActions(codes, context)\n\n// after\nconst context = {\n  items: mapItems(cart.items),        // [] when cart is empty\n  shipping_methods: mapShipping(cart.shipping_methods),\n}\nawait promotionModule.computeActions(codes, context)","handlingStrategy":"validation","validationCode":"const context = { items: ctx.items ?? [], shipping_methods: ctx.shipping_methods ?? [] } as PromotionTypes.ComputeActionContext","typeGuard":"const hasContextKey = (ctx: unknown, key: string): boolean => !!ctx && Array.isArray((ctx as any)[key])","tryCatchPattern":"try { await computeActions(codes, context) } catch (e) { if (e.type === MedusaError.Types.INVALID_DATA && /should be present as an array/.test(e.message)) { /* add the missing key with [] and retry */ } throw e }","preventionTips":["Never omit context keys; use empty arrays","Align promotion target_type with supplied context keys","Centralize context construction in one helper"],"tags":["promotion","compute-actions","context","validation","medusa"],"backgroundTag":"missing-required-context","analyzedSha":"5e06e544a296b9033f20f71f11c559f81a0e5739","analyzedAt":"2026-08-27T07:24:39.599Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}