I am trying to stringify a cyclic object in my typescript. Here is my original code & error:
app.component.ts:
updateList(list: any) {
this.demolist = Array.apply(this, list);
console.log('List - ' + JSON.stringify(this.demolist));
}
TypeError: cyclic object value
I then tried the following code inside my updateList() method:
const getCircularReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
console.log(JSON.stringify(this.demolist, getCircularReplacer()));
}
But now I'm getting this error:
TypeError: toISOString property is not callable
Can someone please show me what changes I need to make to display the contents of my this.listBase?
Here is my HTML:
<upload #fileUpload (listChange)="updateList($event)" data-kind="primary" restrictFiles=".pdf,.doc,.docx">
</upload>
<list #listBase [IncludeComponent]="inputComponent" [list]="demolist">
</list>
The error that you are getting:
TypeError: toISOString property is not callable
Means that somewhere a property named toISOString of some object has been called as if it was a method. e.g.:
someObj.toISOString();
The only method known in Javascript to be built-in and named toISOString is the Date.prototype.toISOString.
JSON.stringify() knows about some standard value types and how to convert (serialize) them to a string. It usually accomplishes that by calling the expected-to-exist toString() method on them. It also knows that the Date object has a toISOString() method and calls that instead whenever it finds one. Therefore in this case I assume that JSON.stringify() encounters an object inside the this.demoList that although is of type Date, yet it has a defined property named toISOString (instead of a method), which overrides the existing Date.prototype.toISOString(), and at that point it tries to call it as a method and you get this error.
I am unable to guess which is this object in your particular case, so I suggest that you should examine all the objects that are inside the this.demoList one by one (and their children) using the developer tools of your browser and find out which one is the "offending" object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With