我正在尝试通过 Restify 客户端执行简单的 POST。这是我正在使用的代码。
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();
当使用 storgie_gen.write_sample_data() 执行代码时;调用它返回并出现以下错误。
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)
API 只是一个快速发布设置,例如
app.post('/ident', routes.ident_create);
路由函数如下所示
exports.ident_create = function (req, res) {
console.log("POST: ");
console.log(req.body);
return res.send(res);
};
我检查了该对象,但我不太确定为什么它会在“{ test: opportunity.d100(), another: opportunity.guid()}”事物对象上出现循环 JSON 错误。为了开始调试,我使用调试器在 WebStorm 中运行上述代码,并且能够从测试中看到以下数据以及上面的另一个变量/键值。结果读作...
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)
我什至更改了上面的代码,这样我就没有使用机会模块并将 var 东西设置为此。
var thing = { test: 'tests', another: '19C80230-89C6-54BD-A9A0-16A1BB77EF27'};
即使将其设置为已知值后,我仍然收到错误。
我仍然不确定什么是循环,而且我也不是特别熟悉追踪它的有效方法 - 但我想我已经到了那里。欢迎任何其他想法。到目前为止,我还没有注意到[通告],但希望很快我会偶然发现一些东西。
最佳答案
我不确定这两件事返回什么:
test: chance.d100(), another: chance.guid()
但是,一个简单的循环引用,至少在 JS 中是这样的:
function Foo() {
this.bar = "Hello World";
this.circ = this;
}
var foo = new Foo();
alert(foo.circ.circ.circ.bar);
我假设某些属性thing
有类似的东西。您无法对循环引用进行字符串化,因为这将是一个无限循环。 Isaacs 有一个名为 json-stringify-safe
的项目,您可能需要使用它来修补您正在使用的库或猴子修补 JSON.stringify。
关于javascript - 使用 Restify 我的帖子显示 "converting circular structure to JSON"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20323934/