0

I'm trying to do a simple CRUD operation in Couchbase from an AngularJS form, but I keep getting this error:

TypeError: Converting circular structure to JSON

Here are the main snippets from AngularJS and Express.js. Any help will be greatly appreciated.

//AngularJS
$http.post('docs', $scope.doc).then(function(res) {
    console.log(res);
}, function(err) {
    console.log(err);
});

//Express.js
router.post('', function(req, res, next) {

    db.upsert('anyname', req.body, function(error, result) {
      if (error) {
        console.log('operation failed', error);
        return;
      }

      res.send(res);
    });

});

Detailed error from terminal:

/Users/name/Workspace/sb-couchbase/node_modules/express/lib/response.js:242
  var body = JSON.stringify(val, replacer, spaces);
                  ^

TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at ServerResponse.json (/Users/name/Workspace/sb-couchbase/node_modules/express/lib/response.js:242:19)
    at ServerResponse.send (/Users/name/Workspace/sb-couchbase/node_modules/express/lib/response.js:151:21)
    at /Users/name/Workspace/sb-couchbase/routes/document.js:36:8
Corey Quillen
  • 1,566
  • 4
  • 24
  • 52

2 Answers2

0

This is not caused by Couchbase, but by the conversion of your object mesh to JSON. If an object a holds a reference to an object b, and b holds a reference to a (maybe via some intermediate objects), an infinite JSON string would result from that.

A variety of solutions for that has been discussed e.g. in Chrome sendrequest error: TypeError: Converting circular structure to JSON.

Community
  • 1
  • 1
TAM
  • 1,731
  • 13
  • 18
0

The problem was a simple Express.js error. The end-point's res.send(res) should be res.send(result). That fixed the problem.

So:

//Express.js
router.post('', function(req, res, next) {

    db.upsert('anyname', req.body, function(error, result) {
      if (error) {
        res.send(error);
        return;
      }

      res.send(result);
    });

});
Corey Quillen
  • 1,566
  • 4
  • 24
  • 52