AUR0500

Error Message

AUR0500: There is no cached controller for the provided ViewModel: <ViewModel>

Where <ViewModel> represents the view model instance for which a controller was expected but not found.

Description

This error occurs when Aurelia attempts to retrieve the controller associated with a specific view model instance from its internal cache using Controller.getCached(viewModel), but no corresponding controller instance is found. This typically indicates an issue with component lifecycle management or an attempt to access a controller for a view model that hasn't been properly instantiated and managed by Aurelia's rendering engine.

Cause

Common causes include:

  1. Accessing Before Initialization: Trying to get the controller for a view model before the component has been fully initialized and rendered by Aurelia (i.e., before its controller has been created and cached).

  2. ViewModel Not Managed by Aurelia: The view model instance provided to Controller.getCached might not be the instance that Aurelia created and associated with a controller. This could happen if you manually instantiate view models outside of the standard component creation process.

  3. Component Already Disposed: Attempting to access the controller after the component and its controller have been disposed and removed from the cache.

  4. Incorrect Instance: Passing the wrong object or an object that isn't actually a view model instance associated with an active Aurelia component.

Solution

  1. Ensure Proper Lifecycle: Access the controller only after the component has been fully initialized. Lifecycle hooks like bound or attached in the component's view model are generally safe places to assume the controller exists.

  2. Use Correct ViewModel Instance: Ensure you are using the exact view model instance managed by Aurelia. If you need access to the controller from outside the view model, consider obtaining it through standard mechanisms like refs (element.au.controller) or dependency injection if appropriate.

  3. Avoid Access After Disposal: Do not try to retrieve the controller for a component that has been deactivated or disposed. Check the component's state if necessary.

  4. Verify the Target: Double-check that the object passed to Controller.getCached (or the object from which you are trying to derive the controller) is indeed the correct, Aurelia-managed view model instance.

Example

import { IPlatform, customElement, ICustomElementController } from 'aurelia';

@customElement({ name: 'my-component', template: '...' })
export class MyComponent {
  public readonly controller!: ICustomElementController<this>; // Injected by Aurelia

  // It's generally unnecessary to manually call Controller.getCached
  // Aurelia injects the controller or makes it available via element refs.

  public attached(initiator: ICustomElementController<this>, parent: ICustomElementController) {
    // 'this.controller' is available here, injected by the framework.
    console.log('Controller accessed safely in attached:', this.controller);

    // --- Example of what might cause the error ---
    // const someOtherVmInstance = new MyComponent();
    // try {
    //   // This WILL throw AUR0500 because 'someOtherVmInstance'
    //   // is not the instance managed by Aurelia for this component rendering.
    //   const cachedController = Controller.getCached(someOtherVmInstance);
    // } catch (e) {
    //   console.error(e); // Outputs AUR0500
    // }
    // ---------------------------------------------
  }
}

// Somewhere else in the application (less common)
// import { Controller } from '@aurelia/runtime-html'; // Adjust import path if needed
//
// function getControllerForElement(element: HTMLElement) {
//   // Prefer using the public API if available
//   const controller = element.au?.controller;
//   if (controller) {
//     console.log('Controller found via element ref:', controller);
//     // If you REALLY needed to use getCached (usually not needed):
//     // const viewModel = controller.viewModel;
//     // try {
//     //   const cachedController = Controller.getCached(viewModel);
//     //   console.log('Controller found via getCached:', cachedController);
//     // } catch(e) {
//     //   console.error("This shouldn't happen if controller was found via element.au", e);
//     // }
//   } else {
//     console.log('Controller not found for element.');
//     // Attempting Controller.getCached with a ViewModel derived some other way
//     // might lead to AUR0500 if the linkage isn't right.
//   }
// }

Debugging Tips

  • Verify the point in the code where Controller.getCached is being called (directly or indirectly). Is it happening too early in the lifecycle?

  • Inspect the view model instance being passed. Is it the correct instance managed by Aurelia? Use console.log or breakpoints.

  • Check if the component associated with the view model has been disposed or removed from the DOM.

  • Consider alternative ways to access the controller if needed, such as using element refs (element.au.controller) which are generally safer and part of the public API. The static Controller.getCached method is often an internal detail.

Last updated

Was this helpful?