找到你要的答案

Q:Node.js : TypeError: Converting circular structure to JSON

Q:Node.js:列表的循环结构转换到JSON

I know it's about circular reference. But I can't find out what's wrong in my code. Following is full error message:

TypeError: Converting circular structure to JSON
at Object.stringify (native)
at Object.DataModel.getDataData (/root/Rhythm-LvDong-Release/models/dataModel.class.js:46:26)
at SocketController.handle (/root/Rhythm-LvDong-Release/controllers/socketController.class.js:647:59)
at Object.SocketRouter.router (/root/Rhythm-LvDong-Release/routes/socketRouter.js:25:28)
at Socket.<anonymous> (/root/Rhythm-LvDong-Release/app.js:190:22)
at Socket.emit (events.js:107:17)
at onReceivePackData (/root/Rhythm-LvDong-Release/app.js:244:24)
at Socket.<anonymous> (/root/Rhythm-LvDong-Release/app.js:154:9)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
/root/Rhythm-LvDong-Release/models/dataModel.class.js:45
    console.log("TESTCASE1:" + detail);

And following is the code: First:dataModel.class.js:46:26

getDataData: function (dataType, detail) {
    var data = {};
    data.type = 'data';
    data.content = {};
    data.content.dataType = dataType;
    if (detail == 0) {
        data.content.detail = 0;
    } else {
        data.content.detail = (detail || {});
    }
    console.log("TESTCASE1:" + detail);
    console.log(JSON.stringify(data));//46th line
    return JSON.stringify(data);
};

Second:socketController.class.js:647:59

var room = GetRoom(socket.roomId);
if (room == null)
    break;
if (!SendMessage(socket.userId, DataModel.getDataData('RefreshRoom', room))) {//the 647th line
}

Then some functions I called:

//SendMessage
function SendMessage(userId, message)
{
    if (!streamSockets[userId])
        return false;
    streamSockets[userId].send(message);
    return true;
}
//GetRoom
function GetRoom(roomId)
{
    var room = null;
    if (roomId >= 0) {
        for (var i in RoomList) {
            if (roomId == RoomList[i].id) {
                room = RoomList[i];
                break;
            }
        }
    }
    return room;
}

Please help me find out what's wrong with my code.

I know it's about circular reference. But I can't find out what's wrong in my code. Following is full error message:

TypeError: Converting circular structure to JSON
at Object.stringify (native)
at Object.DataModel.getDataData (/root/Rhythm-LvDong-Release/models/dataModel.class.js:46:26)
at SocketController.handle (/root/Rhythm-LvDong-Release/controllers/socketController.class.js:647:59)
at Object.SocketRouter.router (/root/Rhythm-LvDong-Release/routes/socketRouter.js:25:28)
at Socket.<anonymous> (/root/Rhythm-LvDong-Release/app.js:190:22)
at Socket.emit (events.js:107:17)
at onReceivePackData (/root/Rhythm-LvDong-Release/app.js:244:24)
at Socket.<anonymous> (/root/Rhythm-LvDong-Release/app.js:154:9)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
/root/Rhythm-LvDong-Release/models/dataModel.class.js:45
    console.log("TESTCASE1:" + detail);

And following is the code: First:dataModel.class.js:46:26

getDataData: function (dataType, detail) {
    var data = {};
    data.type = 'data';
    data.content = {};
    data.content.dataType = dataType;
    if (detail == 0) {
        data.content.detail = 0;
    } else {
        data.content.detail = (detail || {});
    }
    console.log("TESTCASE1:" + detail);
    console.log(JSON.stringify(data));//46th line
    return JSON.stringify(data);
};

第二:socketcontroller JS:647:59。类。

var room = GetRoom(socket.roomId);
if (room == null)
    break;
if (!SendMessage(socket.userId, DataModel.getDataData('RefreshRoom', room))) {//the 647th line
}

然后我调用一些函数:

//SendMessage
function SendMessage(userId, message)
{
    if (!streamSockets[userId])
        return false;
    streamSockets[userId].send(message);
    return true;
}
//GetRoom
function GetRoom(roomId)
{
    var room = null;
    if (roomId >= 0) {
        for (var i in RoomList) {
            if (roomId == RoomList[i].id) {
                room = RoomList[i];
                break;
            }
        }
    }
    return room;
}

请帮我找出我的代码有什么问题。

answer1: 回答1:

Change line:

    data.content.detail = (detail || {});

to:

    data.content.detail = detail || {};

I have been caught by a similar parentheses issue before

换线:

    data.content.detail = (detail || {});

以:

    data.content.detail = detail || {};

我以前被类似的括号问题抓住过

node.js  typeerror