Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught TypeError: Cannot read property 'has' of undefined #86

Open
Lilja opened this issue Jun 22, 2021 · 4 comments
Open

Uncaught TypeError: Cannot read property 'has' of undefined #86

Lilja opened this issue Jun 22, 2021 · 4 comments

Comments

@Lilja
Copy link

Lilja commented Jun 22, 2021

image
image

My usage, I'm using Ref because we are using Vue in this project.

const bus = ref(new Bus())
bus.value.emitter.on("newDataRequest", loadNewDataRequests)

loadNewDataRequests is of signature () => Promise<void>

@Lilja
Copy link
Author

Lilja commented Jun 22, 2021

Removing ref() from the new Bus() call removes the side effect.

Here is the class for reference:

enum Events {
    NewDataRequest = "newDataRequest"
}
export class Bus {
    constructor() {
        this.emitter = new Emittery()
    }
    emitter: Emittery

    onNewDataRequest(callback: () => Promise<void>): void {
        this.emitter.on(Events.NewDataRequest, callback)
    }

    async newDataRequest(): Promise<void> {
       await this.emitter.emit(Events.NewDataRequest)
    }
}

@mattyg
Copy link

mattyg commented May 12, 2023

If anyone else is looking, one solution is to use toRaw():

const bus = ref(new Bus())
toRaw(bus.value).emitter.on("newDataRequest", loadNewDataRequests)

@ThetaSinner
Copy link

I've run into the same issue and documented what is happening here as it relates to the context I'm consuming the library in.

@sindresorhus please could you take a look at this issue and see if you agree with the proposed fix? In this library I'd think that binding any member function that accesses the global maps would be necessary, rather than just the 'on' that is needed in the linked issue. If you're happy with the proposal I'm happy to take on creating a PR for this library.

@georgzoeller
Copy link

Same is happening in alpine.js which uses proxies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants