question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When publishing a value with a circular reference, a 'cyclic object value' error is thrown

See original GitHub issue

When publishing a value with a circular reference, a ‘cyclic object value’ error is thrown. This is because of the serialization that happens to fill topicCache.

Code to reproduce:

const foo = {
  id: "foo",
  bar: ko.observable()
}
const bar = {
  id: "bar",
  foo: ko.observable()
}
ko.postbox.subscribe("foo", function() {
  console.log("foo was published");
})

ko.postbox.publish("foo", foo); // console: foo was published

foo.bar(bar);
ko.postbox.publish("foo", foo); // console: foo was published

bar.foo(foo);
ko.postbox.publish("foo", foo); // TypeError: cyclic object value

Is this something that can be solved by the library, or is it something I need to solve in my code?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:14

github_iconTop GitHub Comments

1reaction
stijnherremancommented, Sep 1, 2016

I’m currently using this workaround:

const serializer = ko.postbox.serializer;
ko.postbox.serializer = (object: any) => { return undefined; }
ko.postbox.publish("selectAdministration", this.administration);
ko.postbox.serializer = serializer;

I’m not using subscribeTo(...), or subscribe(...) with initializeWithLatestValue set to true, so I don’t think I’m breaking anything.

0reactions
frederikprijckcommented, Nov 26, 2016

Well eventually you might hit a point where you do need to serialize the object which includes a circular reference, and removing the serializer won’t help in that situation.

If that’s the case, you may want to look into Douglas Crockford’s cycle.js: https://github.com/douglascrockford/JSON-js

For KO postbox, you could easily create a wrapper around cycle.js and use it to serialize and deserialize your objects containing circular references.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: cyclic object value - JavaScript - MDN Web Docs
The JavaScript exception "cyclic object value" occurs when object references were found in JSON. JSON.stringify() doesn't try to solve them ...
Read more >
Serializing object that contains cyclic object value
Both work by first "decycling" the object, i.e., constructing another object (without any cyclic references) "containing the same ...
Read more >
JavaScript TypeError - Cyclic object value - GeeksforGeeks
This JavaScript exception cyclic object value occurs if the references of objects were found in JSON. JSON.stringify() fails to solve them.
Read more >
TypeError: cyclic object value
To serialize circular references you can use a library that supports them (e.g. cycle.js) or implement a solution by yourself, which will ...
Read more >
Let's play around with the Deep Copy | Frontend Canteen |
It throws an error again. We call that error “cyclic object value” or “circular reference”. It will make an infinity recursion.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found