简体   繁体   English

TypeError:通过快递发送数组时将循环结构转换为 JSON

[英]TypeError: Converting circular structure to JSON when sending array via express

I have a few different files which contain multiple functions that are related, I'm using them as controllers of sorts.我有几个不同的文件,其中包含多个相关的功能,我将它们用作各种控制器。 In one I have a promise and am hitting my catch block when trying to resolve it.其中一个我有一个 promise 并且在尝试解决它时遇到了我的 catch 块。 It is done in a bit of a different way than passing a single value like I most commonly see.它的完成方式与我最常见的传递单个值不同。 I am trying to resolve with a JavaScript object which contains two keys, one with a value of a boolean and the other with an array for a value.我正在尝试使用 JavaScript object 解决,其中包含两个键,一个具有 boolean 的值,另一个具有一个值的数组。

The actual error I am getting in my catch block is我在 catch 块中遇到的实际错误是

TypeError: Converting circular structure to JSON TypeError:将循环结构转换为 JSON

Here is how I am trying to resolve the promise这是我尝试解决 promise 的方法

resolve({success: true, names: body.names});

Where body.names equals the below array其中body.names等于下面的数组

[ [ 'John Smith', 'Jane Doe' ], [ 'John Smith' ] ]

My goal is to have the promise resolve in a way that I can pass my 'success' value to do some extra processing (not an error if false and needs to be handled as a non error) but would also like to be passing the array of names so that I can pass that on in my API response.我的目标是让 promise 以一种我可以传递我的“成功”值来做一些额外处理的方式解析(如果为 false 则不是错误,需要作为非错误处理)但也希望传递数组名称,以便我可以在我的 API 响应中传递它。

EDIT**编辑**

More code更多代码

exports.formatWithNames = function(policy) {
    return new Promise((resolve, reject) => {
        options.json = {policy};
        request.post('/userListByIDs', options, (err, response, body) => {
            if (err) {
                logger.error(`policy: formatWithNames: ${JSON.stringify(err)}`);
                reject({success: false, message: 'Failure'});
            }
            logger.info(`policy: formatWithNames: body: ${JSON.stringify(body)}`);
            resolve({success: true, names: body.names});
        });
    });
}

Then where that is called然后在哪里调用

Policy.find({ companyID: req.body.user.companyID }, (err, policy) => {
        if (err) {
            logger.info(`getAllPolicy: Policy find: Error message: ${err}`);
            return res.status(500).json({ success: false, message: 'Error finding policies.' });
        }

        userController.formatWithNames(policy)
        .then(response => {
            console.log(response)
            return res.status(200).json({ success: true, policy: res });
        })
        .catch(err => {
            logger.error(`getAllPolicy: formatWithNames: Error message: ${err}`);
            return res.json({ success: false, policy: null });
        })


    });

Okay solution was stupidly simple and was just me having a variable miss named, feeling pretty low right now.好的解决方案非常简单,只是我有一个名为miss的变量,现在感觉很低。 I should have used policy: response instead of policy: res when returning my response in my then block of the formatWithNames.我应该使用policy: response而不是policy: res在我 then 的 formatWithNames 块中返回我的响应时。 So what seems to have been happening was res was being sent back but because I was including it in the response it was creating a circular reference to itself (looping?)所以似乎发生的事情是res被发回,但是因为我将它包含在响应中,它正在创建对自身的循环引用(循环?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM