2016-04-23 13 views
0

に円形構造の変換私はAngularJSフォームからのCouchbaseでの単純なCRUD操作をやろうとしているが、私はこのエラーを取得しておいてください。ここではCouchbaseの挿入 - 例外TypeError:JSON

TypeError: Converting circular structure to JSON 

はからの主なスニペットですAngularJSとExpress.js。どんな助けでも大歓迎です。端末から

//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); 
    }); 

}); 

詳細なエラー:

/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 

答えて

0

これはCouchbaseのによって引き起こされるが、あなたのオブジェクトの変換によってJSONにメッシュされていません。オブジェクトaがオブジェクトbへの参照を保持し、bが(おそらくいくつかの中間オブジェクトを介して)aへの参照を保持する場合、無限のJSON文字列がその結果になります。

これに関する様々な解決策が、例えば、 Chrome sendrequest error: TypeError: Converting circular structure to JSONにあります。

0

問題は単純なExpress.jsエラーでした。終点のres.send(res)はres.send(result)でなければなりません。それは問題を解決しました。だから、

//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); 
    }); 

}); 
関連する問題