dotnet/aspnetcore · error

Could not find '${identifier}' ('${key}' was undefined).

Error message

Could not find '${identifier}' ('${key}' was undefined).

What it means

Thrown by findObjectMember while walking the dotted identifier path. For an identifier like 'document.location.href' it walks each key except the last; if any intermediate key is absent (the property is not 'in' the current object, or current is null) it throws, naming the full identifier and the first missing segment.

Source

Thrown at src/JSInterop/Microsoft.JSInterop.JS/src/src/Microsoft.JSInterop.ts:604

   * Traverses the object hierarchy to find an object member specified by the identifier.
   *
   * @param obj Root object to search in.
   * @param identifier Complete identifier of the member to find, e.g. "document.location.href".
   * @returns A tuple containing the immediate parent of the member and the member name.
   */
  export function findObjectMember(obj: any, identifier: string): [any, string] {
      const keys = identifier.split(".");
      let current = obj;

      // First, we iterate over all but the last key. We throw error for missing intermediate keys.
      // Error handling in case of undefined last key depends on the type of operation.
      for (let index = 0; index < keys.length - 1; index++) {
          const key = keys[index];

          if (current && typeof current === "object" && key in current) {
              current = current[key];
          } else {
              throw new Error(`Could not find '${identifier}' ('${key}' was undefined).`);
          }
      }

      return [
          current,
          keys[keys.length - 1]
      ];
  }

  // Takes an object member and a call type and returns a function that performs the operation specified by the call type on the member.
  //
  // @param parent Immediate parent of the accessed object member.
  // @param memberName Name (key) of the accessed member.
  // @param callType The type of the operation to perform on the member.
  // @param identifier The full member identifier. Only used for error messages.
  // @returns A function that performs the operation on the member.
  //
  function wrapJSCallAsFunction(parent: any, memberName: string, callType: JSCallType, identifier: string): Function {

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Verify the identifier string exactly matches the loaded JS API (check spelling, casing, dots).
  2. Ensure the script providing myLib has loaded before the interop call; run interop in OnAfterRenderAsync, not during prerender.
  3. For environment-optional APIs, expose a wrapper JS function that returns a safe default rather than calling the dotted path directly.
  4. Use a try/catch around the interop and fall back gracefully.

Example fix

// before
await JS.InvokeVoidAsync("window.experimentalApi.configure", opts);

// after
await JS.InvokeVoidAsync("myShim.configureExperimental", opts);
// myShim.js
export function configureExperimental(opts) {
  if (window.experimentalApi?.configure) window.experimentalApi.configure(opts);
}
Defensive patterns

Strategy: validation

Validate before calling

// shim that checks the path exists before interop
export function resolve(path) {
  return path.split('.').reduce((o,k) => (o == null ? o : o[k]), window);
}

Type guard

function pathExists(root:any, id:string):boolean {
  return id.split('.').every((k,i,a) => { root = root?.[k]; return root != null || i === a.length-1; });
}

Try / catch

try { await JS.InvokeVoidAsync('lib.deep.fn'); }
catch (e) { if (/Could not find/.test(e.message)) { /* load lib or fallback */ } else throw e; }

Prevention

When it happens

Trigger: JS interop with a dotted identifier where an intermediate property is missing, e.g. JS.InvokeAsync('myLib.notThere.thing') or invoking 'window.someApi.method' when someApi is undefined. Also triggers when the root object itself is null (e.g. a JS object reference whose underlying global was removed).

Common situations: Referencing a browser API that isn't loaded yet (script ordering); typo in an identifier string; library not loaded at invocation time; SSR/prerender where window/document are unavailable; version change removing/renaming a property.

Related errors


AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06). Data as JSON: /api/errors/b770fd11af5b83f3. Report an issue: GitHub.