feathersjs-ecosystem / feathers-sync

Synchronize service events between Feathers application instances
MIT License
221 stars 41 forks source link

Converting circular structure to JSON #140

Closed apmcodes closed 4 years ago

apmcodes commented 4 years ago

Getting the below error since may be after the recent feathers lib update. The error appears randomly at the end of different services. Error goes away on disabling events (as of now app wide) using context.event = null;

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Timeout'
    |     property '_idlePrev' -> object with constructor 'TimersList'
    --- property '_idleNext' closes the circle
    at Object.stringify [as serialize] (<anonymous>)
    at Object.emit (/xxx/node_modules/feathers-sync/lib/core.js:47:46)
    at /xxx/node_modules/@feathersjs/feathers/lib/events.js:16:42
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/xxx/node_modules/@feathersjs/feathers/lib/events.js:16:15)
    at /xxx/node_modules/@feathersjs/feathers/node_modules/@feathersjs/commons/lib/hooks.js:115:46
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

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

    "@feathersjs/authentication-client": "^4.3.11",
    "@feathersjs/authentication-oauth": "^4.3.11",
    "@feathersjs/configuration": "^4.3.11",
    "@feathersjs/errors": "^4.3.11",
    "@feathersjs/express": "^4.3.11",
    "@feathersjs/feathers": "^4.3.11",
    "@feathersjs/socketio": "^4.3.11",
    "@feathersjs/socketio-client": "^4.3.11",
    "feathers-sequelize": "^6.1.0",
    "feathers-sync": "^2.0.0",

NodeJS version: v12.13.0

Not sure if my code is triggering this. Unable to understand as the code was working fine earlier.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Apologies if the issue could not be resolved. FeathersJS ecosystem modules are community maintained so there may be a chance that there isn't anybody available to address the issue at the moment. For other ways to get help see here.

fadiquader commented 4 years ago

I have a similar issue...

Unhandled Rejection at: Promise [object Promise], TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'ClientRequest'
    |     property 'socket' -> object with constructor 'TLSSocket'
    --- property '_httpM...
Leask commented 4 years ago

Same here.

nithin-ideas2it commented 3 years ago

We also facing what is the reason for the issue? Do need to fix anything on data side?

Do we need to use Custom Serializer / Deserializer?

Could some one help?

daffl commented 3 years ago

You just have to make sure that none of your data in the context contain a circular reference. A custom serializer is one option to ensure that, another is to make sure that all params, result or data properties are serializable or serialized (e.g. with their toJSON methods).

navneethkrish commented 3 years ago
Module version: 
"feathers-mongoose": "7.3.1",
"feathers-sync": "2.1.0",
"flatted": "3.0.4" 

 params structure in context:
    params: {
        mongoose: session
     } 

 feather-sync configuration:
    app.configure(  sync({
         uri: `<URL>`,
        serialize: stringify,  // stringify method from flatted package 
        deserialize: parse, // parse method from flatted package 
      }));

I am using feathers mongoose adapter and feathers sync. I have to pass the session in the context to achieve the transaction. which has a circular reference. I used flatted package to avoid converting the circular structure to JSON issue. After the deserialized context from feathers-sync creates session.isTransaction is not a function issue in mongoose library. Is there any other way to store context contain a circular reference like above to achieve sync?

daffl commented 3 years ago

You have to remove the session from the parameters once you used it where it is needed. It is not possible to pass the same session across different instance and it shouldn't be used in an event handler since it should not modify data as described in the caveats section.

navneethkrish commented 3 years ago

thanks @daffl

Artaud commented 3 years ago

I had this issue when I was passing sequelize includes across feathers-sync. A simple fix was to use flatted as the serializer as it can serialize objects with circular references.

import {parse, stringify} from 'flatted';

app.configure(sync({
  uri: app.get('redis'),
  serialize: stringify,
  deserialize: parse
}))