Javascript 如何解决:TypeError:将循环结构转换为JSON。使用Node和express
我试图通过其Id从CosmosDB获取单个项目。我使用Javascript 如何解决:TypeError:将循环结构转换为JSON。使用Node和express,javascript,node.js,express,azure-cosmosdb,Javascript,Node.js,Express,Azure Cosmosdb,我试图通过其Id从CosmosDB获取单个项目。我使用res.send(docResponse)将响应发送给邮递员,但这给了我一个错误,我使用邮递员测试Api的 app.get('/:id', async (req, res) => { try { var id = req.params.id; const dbResponse = await cosmosClient.databases.createIfNotExists({
res.send(docResponse)
将响应发送给邮递员,但这给了我一个错误,我使用邮递员测试Api的
app.get('/:id', async (req, res) => {
try {
var id = req.params.id;
const dbResponse = await cosmosClient.databases.createIfNotExists({
id: databaseId
});
let database = dbResponse.database;
const { container } = await database.containers.createIfNotExists({id: containerId});
const docResponse = await container.item(id, 1).read();
// res.send(docResponse)
res.json(docResponse);
} catch (error) {
console.log(error);
res.status(500).send("Error with database query: " + error.body);
}
})
我也使用了res.json(docResponse)
,但错误是相同的
我认为这是一个错误
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'CosmosClient'
| property 'databases' -> object with constructor 'Databases'
--- property 'client' closes the circle
at JSON.stringify (<anonymous>)
at stringify (C:\Users\abbas.haider\Desktop\node azure\node_modules\express\lib\response.js:1123:12)
at ServerResponse.json (C:\Users\abbas.haider\Desktop\node azure\node_modules\express\lib\response.js:260:14)
at C:\Users\abbas.haider\Desktop\node azure\app.js:92:13
TypeError:将循环结构转换为JSON
-->从构造函数为“CosmosClient”的对象开始
|属性“databases”->具有构造函数“databases”的对象
---属性“客户”结束循环
在JSON.stringify()上
在stringify(C:\Users\abbas.haider\Desktop\node azure\node\u modules\express\lib\response.js:1123:12)
在ServerResponse.json(C:\Users\abbas.haider\Desktop\node azure\node\u modules\express\lib\response.js:260:14)
在C:\Users\abbas.haider\Desktop\node azure\app.js:92:13
尝试res.send(docResponse.resource)代码>而不是?这是我的测试结果
下面是一个关于此错误的示例,根据错误消息,很明显知道docResponse
不是一个合适的响应对象,您需要手动提取响应对象。也许您可以查看这些对象,看看是否有任何答案可以回答您的问题?console.log(docResponse)为您提供了什么?请尝试res.resource@SomeoneSpecial console.log(docResponse)提供正确的输出。错误仅在我尝试res.send时出现。能否尝试在响应中发送docResponse.item?根据文档,read返回一个promise for ItemResponse。阅读(选项?:请求选项):承诺。。这里itemresponse对象有一个属性“item”。