{"record":{"id":"db7af90a931d5aa0","repo":"medusajs/medusa","slug":"shipping-methods-should-be-present-as-an-array-i","errorCode":null,"errorMessage":"\"shipping_methods\" should be present as an array in the context for computeActions","messagePattern":"\"shipping_methods\" 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/shipping-methods.ts","lineNumber":28,"sourceCode":"  ComputedActions,\n  MathBN,\n  MedusaError,\n} from \"@medusajs/framework/utils\"\nimport { Promotion } from \"@models\"\nimport { areRulesValidForContext } from \"../validations\"\nimport { sortShippingLineByPriceAscending } from \"./sort-by-price\"\nimport { computeActionForBudgetExceeded } from \"./usage\"\n\nexport function getComputedActionsForShippingMethods(\n  promotion: PromotionTypes.PromotionDTO | InferEntityType<typeof Promotion>,\n  shippingMethodApplicationContext: PromotionTypes.ComputeActionContext[ApplicationMethodTargetType.SHIPPING_METHODS],\n  methodIdPromoValueMap: Map<string, number>\n): PromotionTypes.ComputeActions[] {\n  let applicableShippingItems: PromotionTypes.ComputeActionContext[ApplicationMethodTargetType.SHIPPING_METHODS] =\n    []\n\n  if (!shippingMethodApplicationContext) {\n    throw new MedusaError(\n      MedusaError.Types.INVALID_DATA,\n      `\"shipping_methods\" should be present as an array in the context for computeActions`\n    )\n  }\n\n  for (const shippingMethodContext of shippingMethodApplicationContext) {\n    const isPromotionApplicableToItem = areRulesValidForContext(\n      promotion.application_method?.target_rules!,\n      shippingMethodContext,\n      ApplicationMethodTargetType.SHIPPING_METHODS\n    )\n\n    if (!isPromotionApplicableToItem) {\n      continue\n    }\n\n    applicableShippingItems.push(shippingMethodContext)\n  }","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/medusajs/medusa/blob/5e06e544a296b9033f20f71f11c559f81a0e5739/packages/modules/promotion/src/utils/compute-actions/shipping-methods.ts#L10-L46","documentation":"getComputedActionsForShippingMethods requires the shipping_methods array in the computeActions context when computing adjustments for shipping-targeted promotions. A missing/null shippingMethodApplicationContext throws INVALID_DATA.","triggerScenarios":"Calling computeActions for a promotion whose application_method.target_type is 'shipping_methods' while the context omits the shipping_methods key (e.g., only items provided), or passes it as null.","commonSituations":"Free-shipping promo codes applied to carts before shipping methods are chosen (context built without shipping data), checkout step ordering — computing adjustments too early, context builders that skip empty shipping arrays.","solutions":["Include shipping_methods: [] (or the mapped cart shipping methods) in the context whenever shipping-targeted codes may be computed","Re-run computeActions after the customer selects a shipping method","Filter passed promotion codes by their target_type to match the context you have"],"exampleFix":"// before\nawait promotionModule.computeActions([\"FREESHIP\"], {\n  items: mapItems(cart.items),\n})\n\n// after\nawait promotionModule.computeActions([\"FREESHIP\"], {\n  items: mapItems(cart.items),\n  shipping_methods: cart.shipping_methods.map((m) => ({\n    id: m.id,\n    amount: m.amount,\n  })),\n})","handlingStrategy":"validation","validationCode":"const context = { items: mapItems(cart.items), shipping_methods: cart.shipping_methods?.map((m) => ({ id: m.id, amount: m.amount })) ?? [] }","typeGuard":"const hasShippingContext = (ctx: unknown): boolean => Array.isArray((ctx as any)?.shipping_methods)","tryCatchPattern":"try { await computeActions(codes, context) } catch (e) { if (e.type === MedusaError.Types.INVALID_DATA && /shipping_methods/.test(e.message)) { /* defer computation until shipping is selected */ } throw e }","preventionTips":["Recompute adjustments after shipping method selection","Always include shipping_methods: []","Filter codes by target_type before computing"],"tags":["promotion","compute-actions","shipping-methods","context","medusa"],"backgroundTag":"missing-required-context","analyzedSha":"5e06e544a296b9033f20f71f11c559f81a0e5739","analyzedAt":"2026-08-27T07:24:39.599Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}