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

TypeError: Cannot read properties of undefined (reading 'toJSON') #186

Closed
palmtown opened this issue Mar 31, 2023 · 4 comments · Fixed by #189
Closed

TypeError: Cannot read properties of undefined (reading 'toJSON') #186

palmtown opened this issue Mar 31, 2023 · 4 comments · Fixed by #189

Comments

@palmtown
Copy link
Contributor

Steps to reproduce

Prior to commit 5106085 I was able to use ctx.service("myservice").emit("created", data) and that would work. However, after the update, it no longer works. The issue is created at service.emit (/usr/app/node_modules/feathers-sync/lib/core.js:55:46) as it does not check to ensure ctx is not undefined.

Is it now a requirement in features to pass the ctx as well when using service.emit? In any case, should there be checking to ensure ctx is not undefined? This seems to be the standard for other code in the same file.

Expected behavior

Event emitted and synced successfully.

Actual behavior

TypeError: Cannot read properties of undefined (reading 'toJSON')
at service.emit (/usr/app/node_modules/feathers-sync/lib/core.js:55:46)

System configuration

Module versions (especially the part that's not working): "3.0.2"

@palmtown
Copy link
Contributor Author

Based on some of the documentation available, it does not require the context to be passed. For example, at https://feathersjs.com/api/events.html, it has an example as show below that doesn't include the context as the third parameter. However, if you follow this example and have feathers-sync enabled, you will get the same error as reported.

What would be the best path forward in this case?

messages.emit('customEvent', { anything: 'Data can be anything' })

@palmtown
Copy link
Contributor Author

palmtown commented Apr 1, 2023

If I add an empty object such as messages.emit('customEvent', { anything: 'Data can be anything' }, {}) it will avoid the error. However, it seems the fix is to check to see if ctx is undefined.

@palmtown
Copy link
Contributor Author

palmtown commented Apr 1, 2023

I added an additional check that resolves and created a pull request at #187.

@palmtown
Copy link
Contributor Author

palmtown commented Apr 1, 2023

If I add an empty object such as messages.emit('customEvent', { anything: 'Data can be anything' }, {}) it will avoid the error. However, it seems the fix is to check to see if ctx is undefined.

Just an F.Y.I., after further testing, if you try to pass an empty object as a third parameter, although the error goes away, feathers emit will not work and you will not receive events on the client side subscribed to the event. The patch I submitted at #187 solves the issue and makes feathers-sync compatiable with featherjs v5.

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

Successfully merging a pull request may close this issue.

1 participant