{"record":{"id":"3ee247daf2ffbb0b","repo":"sveltejs/kit","slug":"requested-can-only-be-called-in-the-context-o","errorCode":null,"errorMessage":"requested(...) can only be called in the context of a command/form remote function","messagePattern":"requested\\(\\.\\.\\.\\) can only be called in the context of a command/form remote function","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/kit/src/runtime/app/server/remote/requested.js","lineNumber":142,"sourceCode":"\tconst ignored = (state.remote.ignored ??= new Set());\n\n\t/** @param {string} payload */\n\tconst consume = (payload) => {\n\t\tpayloads.delete(payload);\n\t\tif (payloads.size === 0) requested?.delete(__.id);\n\t};\n\n\t/** @param {string} payload */\n\tconst create_ignore = (payload) => () => {\n\t\tignored.add(create_remote_key(__.id, payload));\n\t};\n\n\t// note: don't initialize these maps here -- they will be initialized by the\n\t// command/form wrapper when we enter them, and if we initialize them here\n\t// we will enable requested(...) in contexts where it shouldn't be allowed,\n\t// such as load functions or other server functions\n\tif (!state.is_in_remote_form_or_command) {\n\t\tthrow new Error(\n\t\t\t'requested(...) can only be called in the context of a command/form remote function'\n\t\t);\n\t}\n\tconst [selected, skipped] = split_limit([...payloads], limit);\n\n\t/**\n\t * Registers the failure exactly like `.set()` registers a value: the error record\n\t * is serialized to the client (putting the query there into a failed state), and\n\t * subsequent server-side calls of the query with the same argument reject with it.\n\t * @param {string} payload\n\t * @param {unknown} error\n\t */\n\tconst record_failure = (payload, error) => {\n\t\tconst promise = Promise.reject(error);\n\t\tpromise.catch(noop);\n\n\t\tget_cache(__, state)[payload] = promise;\n\t\trefresh(event, state, __, payload, () => promise);","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/kit/src/runtime/app/server/remote/requested.js#L124-L160","documentation":"requested() reads payloads recorded by a command or form remote function while it runs. It is only valid inside those wrappers; the request state flag is_in_remote_form_or_command is initialized when entering them, and calls elsewhere are rejected.","triggerScenarios":"Calling requested(...) in a load function, a plain query, a module-level server file, or any server context that is not inside an executing command/form remote function.","commonSituations":"Hoisting requested() out of a command into load; calling it inside another query's validator; trying to inspect other queries in module scope at startup.","solutions":["Move the requested(...) call inside the command or form remote function body","If needed in load, have the command return the data and consume it there","Restructure so query tracking happens during the command execution window"],"exampleFix":"// before\nexport const load = async () => {\n  const pending = await requested(getPosts, 5); // invalid context\n};\n// after\nexport const createPost = command(async (input) => {\n  const pending = await requested(getPosts, 5);\n  /* ... */\n});","handlingStrategy":"validation","validationCode":"// ensure the call site is inside a command/form body\nexport const myCommand = command(async (input) => {\n  // requested(...) is valid only here\n});","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Call requested() only inside command/form remote functions","Never hoist requested() to load functions or module scope","Review code moves that relocate requested() calls"],"tags":["remote-functions","requested","command","context"],"backgroundTag":"api-called-out-of-context","analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}