我知道它是关于循环引用的。但我无法找到代码中的错误。 以下是完整的错误消息:
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);
以下是代码: 第一: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.class.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;
}
请帮我查一下我的代码有什么问题。
答案 0 :(得分:0)
更改行:
data.content.detail = (detail || {});
为:
data.content.detail = detail || {};
我之前被类似的括号问题抓住了