So I am making a file tree list structure in the front end and it produces a JSON tree. Every time I modify the tree I post the object to the backend in order to store the new leafs that I add. Here is the tree structure that I am passing to the backend
{
"0": {
"id": "1",
"isLeaf": true,
"_id": "626584371ff07c474d6da2b3",
"pid": 0,
"isRoot": true,
"isActive": true,
"type": "testSite2",
"name": "testSite2",
"ownerUid": "XeqTkJ5VUnWFYWK7bKWXeQ58yLF3",
"createdAt": "2022-04-24T17:09:11.877Z",
"updatedAt": "2022-04-24T17:09:11.877Z",
"__v": 0
},
"1": {
"id": "3",
"isLeaf": false,
"_id": "626584a31ff07c474d6da2f3",
"pid": 0,
"isRoot": true,
"isActive": true,
"type": "testSite2",
"name": "todelete",
"ownerUid": "XeqTkJ5VUnWFYWK7bKWXeQ58yLF3",
"createdAt": "2022-04-24T17:10:59.897Z",
"updatedAt": "2022-04-24T17:10:59.897Z",
"__v": 0,
"children": [
{
"isLeaf": true,
"name": "test",
"ownerUiD": "XeqTkJ5VUnWFYWK7bKWXeQ58yLF3",
"type": "testType",
"id": 1650988619073,
"isRoot": "false",
"isActive": "true",
"pid": "3"
}
]
}
}
When I send this as is to the backend it returns an error 400 so I try to resolve by parsing it before I pass, whenever I do JSON.parse() on the structure it returns this error:
SyntaxError: Unexpected token o in JSON at position 1
I tried verifying my JSON structure and it passes as a valid JSON, I tried to stringify it and it returns the same error. What am I missing here?
The reason has to be because the method expects a string and you have returned a object to it ,thats when this error occurs.Do check your code for any conversions from object to string in previous lines.If what you require is for the data to be sent as a object instead of a string,you can send it without parsing it.