medusajs/medusa · error · MedusaError
"${contextKey}" should be present as an array in the context
Error message
"${contextKey}" should be present as an array in the context for computeActions What it means
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.
Source
Thrown at packages/modules/promotion/src/utils/compute-actions/line-items.ts:26
ApplicationMethodAllocation,
ApplicationMethodTargetType,
calculateAdjustmentAmountFromPromotion,
ComputedActions,
MathBN,
MedusaError,
ApplicationMethodTargetType as TargetType,
} from "@medusajs/framework/utils"
import { Promotion } from "@models"
import { areRulesValidForContext } from "../validations"
import { sortLineItemByPriceAscending } from "./sort-by-price"
import { computeActionForBudgetExceeded } from "./usage"
function validateContext(
contextKey: string,
context: PromotionTypes.ComputeActionContext[TargetType]
) {
if (!context) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`"${contextKey}" should be present as an array in the context for computeActions`
)
}
}
export function getComputedActionsForItems(
promotion: PromotionTypes.PromotionDTO | InferEntityType<typeof Promotion>,
items: PromotionTypes.ComputeActionContext[TargetType.ITEMS],
appliedPromotionsMap: Map<string, number>,
allocationOverride?: ApplicationMethodAllocationValues
): PromotionTypes.ComputeActions[] {
validateContext("items", items)
return applyPromotionToItems(
promotion,
items,
appliedPromotionsMap,View on GitHub (pinned to 5e06e544a2)
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
Example fix
// before
const context = {}
if (cart.items.length) context.items = mapItems(cart.items)
await promotionModule.computeActions(codes, context)
// after
const context = {
items: mapItems(cart.items), // [] when cart is empty
shipping_methods: mapShipping(cart.shipping_methods),
}
await promotionModule.computeActions(codes, context) Defensive patterns
Strategy: validation
Validate before calling
const context = { items: ctx.items ?? [], shipping_methods: ctx.shipping_methods ?? [] } as PromotionTypes.ComputeActionContext Type guard
const hasContextKey = (ctx: unknown, key: string): boolean => !!ctx && Array.isArray((ctx as any)[key])
Try / catch
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 } Prevention
- Never omit context keys; use empty arrays
- Align promotion target_type with supplied context keys
- Centralize context construction in one helper
When it happens
Trigger: 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.
Common situations: Conditionally building context and skipping empty arrays (setting undefined instead of []), target_type misconfigured on the promotion, generic computeActions wrappers that forward partial contexts.
Related errors
- "items" should be present as an array in the context to comp
- "shipping_methods" should be present as an array in the cont
- Cannot add promotions to campaign where currency_code don't
- Application Method value should be a percentage number betwe
- apply_to_quantity is a required field for Promotion type of
AI-assisted analysis of medusajs/medusa@5e06e544a2 (2026-08-27).
Data as JSON: /api/errors/9b49c3b037c6a304.
Report an issue: GitHub.