Primeng: p-tree : TypeError: Converting circular structure to JSON

Created on 4 May 2017  路  5Comments  路  Source: primefaces/primeng

I'm submitting a ... (check one with "x")

[X] bug report => Search github for a similar issue or PR before submitting
[ ] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35

Current behavior
myTreeObjs : TreeNode[]
JSON.stringify(myTreeObjs) generates JSON string without any error

Expected behavior
Able to perform JSON.stringify(myTreeObj) without getting any error.

Minimal reproduction of the problem with instructions
When I am trying to convert TreeNode object to JSON string using
JSON.stringify(myTreeObj) to send to my server, I get the following error

"ERROR TypeError: Converting circular structure to JSON"

What is the motivation / use case for changing the behavior?
I want to use priming Tree Module to get and post data to and from server

Angular version:** 4.0.0

PrimeNG version: 4.0.0-rc.2

Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]

  • **Language: TypeScript X.X

  • Node (for AoT issues): node --version = v6.10.0

Most helpful comment

@nikitadm did u find the solution? I'm also facing the same issue.
@cagataycivici can you please explain how to set it? I tried but it's not working.

All 5 comments

Before stringify you need to set the parent of each node to null, I'm afraid we can't think of any other way to solve it.

@nikitadm did u find the solution? I'm also facing the same issue.
@cagataycivici can you please explain how to set it? I tried but it's not working.

@sandu-ns Did you figure this out? I'm having the same issue when I try to update the treenode. The weird thing is the most time it is working, but sometimes the convert stops because parent is undefined.
I still didn't find a way to set parent to null.

I think I know the problem you are facing:
-> You are accessing node.parent in your HTML code, that's why p-tree component creates parent nodes.
-> The solution would be as follow; You need to deep-copy/clone the tree first (Object.assign(target, source) does not work! etc), then with a recursive function, traverse the cloned tree and delete node.parent in each iteration, afterwards you can stringify the cloned and pruned new tree.

It would be nice if the p-tree implementation would not set the parent attribute for each node.

I guess it is only implemented because it is handy to be able to traverse the tree in both ways via simple properties. But I dont see why it would not be possible to code it without that.

@cagataycivici Are PRs for this welcome?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Helayxa picture Helayxa  路  3Comments

garethlewis picture garethlewis  路  3Comments

KannanMuruganmony picture KannanMuruganmony  路  3Comments

miresk picture miresk  路  3Comments

papiroca-tm picture papiroca-tm  路  3Comments