With restify my post is showing a "converting circular structure to JSON" error

StackOverflow https://stackoverflow.com/questions/20323934

  •  07-08-2022
  •  | 
  •  

سؤال

I'm trying to do a simple POST via a restify client. Here's the code I'm using.

    var gengie = require('chance'),
    assert = require('assert'),
    restify = require('restify'),
    chance = new gengie();

var storgie_gen = {

    write_sample_data: function (){
        var client = restify.createJsonClient({
            url: 'http://localhost:3000'
        });

        var thing = { test: chance.d100(), another: chance.guid()};

        client.post('/ident/', thing, function(err, req, res, obj){
            assert.ifError(err);
            console.log('%d -> %j', res.statusCode, res.headers);
            console.log('%j', obj);
        });
    }
};

storgie_gen.write_sample_data();

When the code is executed with the storgie_gen.write_sample_data(); call it returns with the following error.

assert.js:324
assert.ifError = function(err) { if (err) {throw err;}};
^
InternalServerError: {"error":{"message":"Converting circular structure to JSON","stack":"TypeError: Converting circular structure to JSON\n    at Object.stringify (native)\n    at ServerResponse.res.json (/Users/adronhall/Coderz/Storgie/node_modules/express/lib/response.js:189:19)\n    at ServerResponse.res.send (/Users/adronhall/Coderz/Storgie/node_modules/express/lib/response.js:121:21)\n    at exports.ident_create (/Users/adronhall/Coderz/Storgie/routes/index.js:14:16)\n    at callbacks (/Users/adronhall/Coderz/Storgie/node_modules/express/lib/router/index.js:164:37)\n    at param (/Users/adronhall/Coderz/Storgie/node_modules/express/lib/router/index.js:138:11)\n    at pass (/Users/adronhall/Coderz/Storgie/node_modules/express/lib/router/index.js:145:5)\n    at Router._dispatch (/Users/adronhall/Coderz/Storgie/node_modules/express/lib/router/index.js:173:5)\n    at Object.router (/Users/adronhall/Coderz/Storgie/node_modules/express/lib/router/index.js:33:10)\n    at next (/Users/adronhall/Coderz/Storgie/node_modules/express/node_modules/connect/lib/proto.js:193:15)"}}
at ClientRequest.onResponse (/Users/adronhall/Coderz/Storgie/node_modules/restify/lib/clients/http_client.js:132:38)
at ClientRequest.g (events.js:175:14)
at ClientRequest.EventEmitter.emit (events.js:95:17)
at HTTPParser.parserOnIncomingClient (http.js:1658:21)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:119:23)
at Socket.socketOnData (http.js:1553:20)
at TCP.onread (net.js:524:27)

The API is simply an express post setup like

app.post('/ident', routes.ident_create);

with the route function looking like this

exports.ident_create = function (req, res) {

    console.log("POST: ");
    console.log(req.body);

    return res.send(res);
};

I checked out the object, but I'm not exactly sure why it would be getting a circular JSON error on the "{ test: chance.d100(), another: chance.guid()}" thing object. To begin the debugging I run the above code in WebStorm using the debugger and was able to see the following data from the test and another variables/key values above. The results read as...

    debugger listening on port 65377
storgie server listening on port 3000
POST:
{ test: 72, another: '19C80230-89C6-54BD-A9A0-16A1BB77EF27' }
TypeError: Converting circular structure to JSON
POST /ident/     at Object.stringify (native)
500     at ServerResponse.res.json (/Users/adron/Codez/Storgie/node_modules/express/lib/response.js:189:19)
159821ms    at ServerResponse.res.send (/Users/adron/Codez/Storgie/node_modules/express/lib/response.js:121:21)

at exports.ident_create (/Users/adron/Codez/Storgie/routes/index.js:14:16)
at callbacks (/Users/adron/Codez/Storgie/node_modules/express/lib/router/index.js:164:37)
at param (/Users/adron/Codez/Storgie/node_modules/express/lib/router/index.js:138:11)
at pass (/Users/adron/Codez/Storgie/node_modules/express/lib/router/index.js:145:5)
at Router._dispatch (/Users/adron/Codez/Storgie/node_modules/express/lib/router/index.js:173:5)
at Object.router (/Users/adron/Codez/Storgie/node_modules/express/lib/router/index.js:33:10)
at next (/Users/adron/Codez/Storgie/node_modules/express/node_modules/connect/lib/proto.js:193:15)

I even changed the code above so that I wasn't using the chance module and set var thing to this.

var thing = { test: 'tests', another: '19C80230-89C6-54BD-A9A0-16A1BB77EF27'};

Even after setting it to known values, I still get the error.

I'm still not sure what is circular, and I'm not particularly familiar with efficient ways to trace this down - but I think I'm getting there. Any other ideas welcome. I haven't, as of yet, noticed a [Circular] but hopefully soon I'll stumble onto something.

هل كانت مفيدة؟

المحلول 2

After a ton of troubleshooting the wrong variable for a circular struture, I realized how simple the issue was.

The post API end point code looked like this.

exports.ident_create = function (req, res) {
return res.send(res);};

Sometimes, it's the little oversights that kick one in the ass. The res.send(res) should be res.send('result'); of sorts instead of returning itself. Grrrr.

نصائح أخرى

I'm not sure what these two things return:

test: chance.d100(), another: chance.guid()

But, a simple circular reference, at least in JS is something like:

function Foo() {
  this.bar = "Hello World";
  this.circ = this;
}

var foo = new Foo();
alert(foo.circ.circ.circ.bar);

I'm assuming some property thing has something like that. You are unable to stringify circular references because, well, it'd be an infinite loop. Isaacs has a project called json-stringify-safe which you may need to patch the library you're using with or monkey patch JSON.stringify.

https://github.com/isaacs/json-stringify-safe

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top