Search code examples
couchdbcloudantcouchdb-futon

CouchDB "[Circular]" when writing to an array


In CouchDB, I am writing to an array and keep getting the message "[Circular]". I am using Node.js to create the data to be written like this.

Say I have an two email objects in the same document in CouchDB:

unverifiedEmail = [{"address":"[email protected]","dateAdded":"1389215329484"}]
verifiedEmail = []

Now in Node.js I do this before writing.

var oldData = readFromCouchDb();
var newData = oldData;
newData.verifiedEmail.unshift(newData.unverifiedEmail[0]);
writeToCouchDb(newData);

Then when I view the document in Futon I see this:

unverifiedEmail = [{"address":"[email protected]","dateAdded":"1389215329484"}]
verifiedEmail = "[Circular]"

What's going on here?


Solution

  • I found out the issue has to do with the depth of the way to set Javascript objects equal to each other.

    To solve this, I used the following code in place of the unshift above:

    newData.verifiedEmail.unshift(JSON.parse(JSON.stringify(newData.unverifiedEmail[0])));