aurelia/polyfills

@singleton view-model fails to load

Closed this issue · 10 comments

Take the skeleton-navigation, just add @singleton to e.g. users.js and navigating to /users will throw this error:

ERROR [app-router] Error: Error invoking . Check the inner error for details.
------------------------------------------------
inner error: TypeError: Cannot read property 'hasOwnProperty' of undefined
    at Object.Reflect.defineMetadata (http://localhost:9002/jspm_packages/npm/aurelia-polyfills@1.0.0-beta.1.0.6/aurelia-polyfills.js:750:37)
    at Object.define (http://localhost:9002/jspm_packages/npm/aurelia-metadata@1.0.0-beta.1.1.6/aurelia-metadata.js:34:15)
    at new <anonymous> (http://localhost:9002/jspm_packages/npm/aurelia-dependency-injection@1.0.0-beta.1.1.5/aurelia-dependency-injection.js:257:33)
    at Object.invoke (http://localhost:9002/jspm_packages/npm/aurelia-dependency-injection@1.0.0-beta.1.1.5/aurelia-dependency-injection.js:364:14)
    at InvocationHandler.invoke (http://localhost:9002/jspm_packages/npm/aurelia-dependency-injection@1.0.0-beta.1.1.5/aurelia-dependency-injection.js:339:168)
    at Container.invoke (http://localhost:9002/jspm_packages/npm/aurelia-dependency-injection@1.0.0-beta.1.1.5/aurelia-dependency-injection.js:587:25)
    at StrategyResolver.get (http://localhost:9002/jspm_packages/npm/aurelia-dependency-injection@1.0.0-beta.1.1.5/aurelia-dependency-injection.js:145:37)
    at Container.get (http://localhost:9002/jspm_packages/npm/aurelia-dependency-injection@1.0.0-beta.1.1.5/aurelia-dependency-injection.js:524:23)
    at http://localhost:9002/jspm_packages/npm/aurelia-templating@1.0.0-beta.1.1.4/aurelia-templating.js:3971:73
    at Object.Reflect.defineMetadata (http://localhost:9002/jspm_packages/npm/aurelia-polyfills@1.0.0-beta.1.0.6/aurelia-polyfills.js:750:37)
    at Object.define (http://localhost:9002/jspm_packages/npm/aurelia-metadata@1.0.0-beta.1.1.6/aurelia-metadata.js:34:15)
    at new <anonymous> (http://localhost:9002/jspm_packages/npm/aurelia-dependency-injection@1.0.0-beta.1.1.5/aurelia-dependency-injection.js:257:33)
    at Object.invoke (http://localhost:9002/jspm_packages/npm/aurelia-dependency-injection@1.0.0-beta.1.1.5/aurelia-dependency-injection.js:364:14)
    at InvocationHandler.invoke (http://localhost:9002/jspm_packages/npm/aurelia-dependency-injection@1.0.0-beta.1.1.5/aurelia-dependency-injection.js:339:168)
    at Container.invoke (http://localhost:9002/jspm_packages/npm/aurelia-dependency-injection@1.0.0-beta.1.1.5/aurelia-dependency-injection.js:587:25)
    at StrategyResolver.get (http://localhost:9002/jspm_packages/npm/aurelia-dependency-injection@1.0.0-beta.1.1.5/aurelia-dependency-injection.js:145:37)
    at Container.get (http://localhost:9002/jspm_packages/npm/aurelia-dependency-injection@1.0.0-beta.1.1.5/aurelia-dependency-injection.js:524:23)
    at http://localhost:9002/jspm_packages/npm/aurelia-templating@1.0.0-beta.1.1.4/aurelia-templating.js:3971:73
    at new AggregateError (http://localhost:9002/jspm_packages/npm/aurelia-pal@1.0.0-beta.1.1.1/aurelia-pal.js:20:13)
    at Container.invoke (http://localhost:9002/jspm_packages/npm/aurelia-dependency-injection@1.0.0-beta.1.1.5/aurelia-dependency-injection.js:589:15)
    at StrategyResolver.get (http://localhost:9002/jspm_packages/npm/aurelia-dependency-injection@1.0.0-beta.1.1.5/aurelia-dependency-injection.js:145:37)
    at Container.get (http://localhost:9002/jspm_packages/npm/aurelia-dependency-injection@1.0.0-beta.1.1.5/aurelia-dependency-injection.js:524:23)
    at http://localhost:9002/jspm_packages/npm/aurelia-templating@1.0.0-beta.1.1.4/aurelia-templating.js:3971:73

This appears to already be fixed in master. Release coming tomorrow.

Same error also when annotating with @transient, btw. Is the release coming out today also switching to Babel 6?

I don't know if this was intended to be fixed in 1.0.0-beta.1.1.0, but I'm still experiencing this.

If you are, there's something peculiar about your scenario I think. We did test this. We will need a repro to see it happening.

I'm just adding @singleton in the latest skeleton users.js VM, like this:

import {singleton, inject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-fetch-client';
import 'fetch';

@singleton
@inject(HttpClient)
export class Users {
    // ...
}

then navigating to "Github Users" throws the error. I can pack a repro of this, but it is really a clean clone from the skeleton, npm install and jspm update, added @singleton and gulp watch.
Maybe it's something in my outside-app environment, or browser (I'm on latest Chrome)?

Try this singleton() <- parens required.

That was it. I need to document myself better on decorators… Now it loads, but the routes screwed up, I guess the injected Router isn't the same as before! But that's another story. Thank you Rob!

It's not your fault on the decorators side. Some of them require parens and some don't. It's kind of an awkward part of the spec that it depends on how the deco is written which way it needs to be called. We've tried to make ours work either way and "bullet proof" them but it's possible with singleton. We'll have this in the upcoming DI docs so it's clear.

5 years later...

This happens to me every now and then. And of course, every time I forget what the culprit is. After ~1h of debugging, I slap my forehead. This probably has happened to me at least 5-7 times by now.

Is it possible to make error message for this particular case more user-friendly? It would save everyone a lot of time.