Skip to content

Instantly share code, notes, and snippets.

@fredarend
Last active February 16, 2021 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredarend/fcdf011502eecb0f2675e19fce093b62 to your computer and use it in GitHub Desktop.
Save fredarend/fcdf011502eecb0f2675e19fce093b62 to your computer and use it in GitHub Desktop.
Converting circular structure to JSON\
import Video from '../../models/Video';
import WatchedVideo from '../../models/WatchedVideo';
class ModuleVideoController {
async index(req, res) {
const { module_id, student_id } = req.params;
// Busco os vídeos cadastrados no banco de dados.
const videos = await Video.findAll({
attributes: ['id', 'title', 'url'],
where: [{ module_id }],
});
// Busco os vídeos assistidos pelo usuário "student_id".
const watched = await WatchedVideo.findAll({
attributes: ['video_id'],
where: [{ module_id, student_id }],
});
// Função para incluir o watched=true ou false.
const watched_video = videos.map((v) => {
if (watched.some((w) => w.video_id === v.id))
return { ...v, watched: true };
return { ...v, watched: false };
});
// Quando efetuo o teste através do postman, recebo a mensagem no final do arquivo.
return res.json({ watched_video });
}
}
export default new ModuleVideoController();
/* ====================================================== */
//MENSAGEM DE ERRO
{
"error": {
"message": "Converting circular structure to JSON\n --> starting at object with constructor 'Sequelize'\n | property 'dialect' -> object with constructor 'PostgresDialect'\n --- property 'sequelize' closes the circle",
"name": "TypeError",
"frames": [
{
"file": "node_modules\\express\\lib\\response.js",
"filePath": "C:\\MyApps\\efabet\\efabet-backend\\node_modules\\express\\lib\\response.js",
"method": "stringify",
"line": 1123,
"column": 12,
"context": {
"start": 1118,
"pre": "function stringify (value, replacer, spaces, escape) {\n // v8 checks arguments.length for optimizing simple call\n // https://bugs.chromium.org/p/v8/issues/detail?id=4730\n var json = replacer || spaces\n ? JSON.stringify(value, replacer, spaces)",
"line": " : JSON.stringify(value);",
"post": "\n if (escape) {\n json = json.replace(/[<>&]/g, function (c) {\n switch (c.charCodeAt(0)) {\n case 0x3c:"
},
"isModule": true,
"isNative": false,
"isApp": false
},
{
"file": "node_modules\\express\\lib\\response.js",
"filePath": "C:\\MyApps\\efabet\\efabet-backend\\node_modules\\express\\lib\\response.js",
"method": "ServerResponse.json",
"line": 260,
"column": 14,
"context": {
"start": 255,
"pre": " // settings\n var app = this.app;\n var escape = app.get('json escape')\n var replacer = app.get('json replacer');\n var spaces = app.get('json spaces');",
"line": " var body = stringify(val, replacer, spaces, escape)",
"post": "\n // content-type\n if (!this.get('Content-Type')) {\n this.set('Content-Type', 'application/json');\n }"
},
"isModule": true,
"isNative": false,
"isApp": false
},
{
"file": "src\\app\\controllers\\student_platform\\ModuleVideoController.js",
"filePath": "C:\\MyApps\\efabet\\efabet-backend\\src\\app\\controllers\\student_platform\\ModuleVideoController.js",
"method": "index",
"line": 27,
"column": 16,
"context": {
"start": 22,
"pre": "\n if (!watched_video.length) {\n return res.json({ message: `Videos not found in module: ${module_id}` });\n }\n",
"line": " return res.json({ watched_video });",
"post": " }\n}\n\nexport default new ModuleVideoController();\n"
},
"isModule": false,
"isNative": false,
"isApp": true
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment