Reputation: 3
I am trying to append the ID of a setInterval
function to a JSON file, but it gets me an error TypeError: Converting circular structure to JSON
My code looks like this
var clearID = setInterval(function(){
//do stuff
},(60)*1000);
fs.readFile('path', 'utf8', function readFileCallback(err, data){
if (err) return console.log(err);
var doc = JSON.parse(data);
doc.IDs.shift();
doc.IDs.push(clearID);
var saved = JSON.stringify(doc);
fs.writeFile('path', saved, 'utf8', (err)=>{
if(err) return console.log(err);
});
});
What should I change to stop this error?
Upvotes: 0
Views: 93
Reputation: 55792
In a NodeJS environment, you simply can't convert the return value of a setInterval
call to JSON string. The error you get is very clear, the return value contains one or more circular references.
Try this (in a node environment):
var x = setInterval(function() { },100);
console.log(x);
See the circular references? They show up as [Circular]
In a browser environment, you'd get a number returned from setTimeout
or setInterval
, not the case when these functions are executed in node.
Browser timers documentation (e.g. setTimeout)
Upvotes: 1