0

I'm getting this error

Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'history' -> object with constructor 'Object'
    |     property 'previousData' -> object with constructor 'Array'
    --- index 0 closes the circle

for these code lines. Need help in understanding the error as well as rectifying it.

const data = req.payload['profileData'];
data.history = {
        lastUpdated: [currenttimestamp],
        previousData: [data]
};

If I JSON.stringify(data) after this I get the above error. I'm also getting the error if I save this in database. If I console.log(data) directly, I get [object object] response. (I don't understand this response too. Why is it giving an array of objects, when the the payload itself is a JSON.) req.payload['profileData'] is of this sort:

{
  "color": "#A020F0",
  "name": "Eggplant",
  "scientificName": "Solanum melongena"
}

Is there a way to directly assign the data to previousData without de-structuring?

SunAns
  • 193
  • 1
  • 2
  • 15
  • 1
    You have a reference to `data` inside `data.history.previousData`. So, when you stringify it will throw an error. You could probably save history in another place or create a clone `previousData: [{ ...data }]`. – adiga Nov 17 '22 at 08:35
  • I did the latter and it solved my problem :) Can you also answer this part? "If I console.log(data) directly, I get [object object] response..." – SunAns Nov 17 '22 at 08:51
  • something like `console.log(JSON.stringify(data, null, 4))` or you can refer to these [How can I get the full object in Node.js's console.log(), rather than '\[Object\]'?](https://stackoverflow.com/questions/10729276) and [console.log returns \[Object\] when object is in multiple other objects](https://stackoverflow.com/questions/61788830) – adiga Nov 17 '22 at 09:19
  • No I meant why do I get [object object] when I console.log(data)? – SunAns Nov 17 '22 at 09:20

0 Answers0