{"record":{"id":"4954bbd8dfb20655","repo":"usebruno/bruno","slug":"creating-a-variable-without-specifying-a-name-is-n","errorCode":null,"errorMessage":"Creating a variable without specifying a name is not allowed.","messagePattern":"Creating a variable without specifying a name is not allowed\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/bruno-js/src/bru.js","lineNumber":311,"sourceCode":"      this.oauth2CredentialsToReset.push(credentialId);\n    }\n\n    // Remove matching credential variables so subsequent getOauth2CredentialVar() calls return undefined\n    const prefix = `$oauth2.${credentialId}.`;\n    for (const key of Object.keys(this.oauth2CredentialVariables)) {\n      if (key.startsWith(prefix)) {\n        delete this.oauth2CredentialVariables[key];\n      }\n    }\n  }\n\n  hasVar(key) {\n    return Object.hasOwn(this.runtimeVariables, key);\n  }\n\n  setVar(key, value) {\n    if (!key) {\n      throw new Error('Creating a variable without specifying a name is not allowed.');\n    }\n\n    if (variableNameRegex.test(key) === false) {\n      throw new Error(\n        `Variable name: \"${key}\" contains invalid characters!`\n        + ' Names must only contain alpha-numeric characters, \"-\", \"_\", \".\"'\n      );\n    }\n\n    if (!Object.hasOwn(this.runtimeVariables, key) || !isEqual(this.runtimeVariables[key], value)) {\n      this.runtimeVariables[key] = value;\n      this._runtimeVarsDirty = true;\n    }\n  }\n\n  getVar(key) {\n    if (variableNameRegex.test(key) === false) {\n      throw new Error(","sourceCodeStart":293,"sourceCodeEnd":329,"githubUrl":"https://github.com/usebruno/bruno/blob/9bdd81c7bdc57006e5f5ebffb79321a8d979f712/packages/bruno-js/src/bru.js#L293-L329","documentation":"Thrown by bru.setVar(key, value) when the key argument is falsy. This guards the runtime (request-scoped) variable scope. The check at bru.js:310 rejects undefined, null, empty string, 0, and false before validating the key format and writing to runtimeVariables.","triggerScenarios":"Calling bru.setVar(undefined, 'value'), bru.setVar('', 'value'), or bru.setVar(null, 'value'). This is common when the key is derived from response data that is not present or from a loop variable that is empty.","commonSituations":"Using a response field as the key name when the field is absent. Iterating over a list with empty entries and using each as a key. Forgetting to pass the first argument.","solutions":["Pass a non-empty string literal as the key: bru.setVar('userId', value).","Guard the call with a truthiness check on the key.","Use a default key when the dynamic source may be empty: bru.setVar(key || 'default', value)."],"exampleFix":"// before\nfor (const item of items) {\n  bru.setVar(item.name, item.value); // item.name may be undefined\n}\n\n// after\nfor (const item of items) {\n  if (item.name) {\n    bru.setVar(item.name, item.value);\n  }\n}","handlingStrategy":"validation","validationCode":"function isValidVarKey(key) {\n  return typeof key === 'string' && key.length > 0;\n}\n// before calling: if (isValidVarKey(key)) bru.setVar(key, value);","typeGuard":null,"tryCatchPattern":"try {\n  bru.setVar(key, value);\n} catch (e) {\n  if (e.message.includes('without specifying a name')) {\n    console.error('Runtime variable key was empty or missing');\n  }\n}","preventionTips":["Always pass a non-empty string as the key.","Guard dynamic keys from loops or response data with a truthiness check.","Use a fallback key when the source may be empty."],"tags":["variables","validation","setvar","empty-key"],"backgroundTag":null,"analyzedSha":"9bdd81c7bdc57006e5f5ebffb79321a8d979f712","analyzedAt":"2026-08-13T04:09:25.751Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}