I am using the following function as middleware and want to send the length of the array (req.session.songArray.length) as a JSON however any JSON I attempt to send through res gives the error below. I understand that circular objects cannot be JSON stringified but just trying to use { num: 20 } gives this error and I am not sure why. When I remove the res.status.json line the code runs without errors. I am using npm packages genius-lyrics and express-session for insight.
async function initSongArray(req, res, next){
const searches = await Client.songs.search(req.params.artist);
let songNum = 0
while((searches[songNum].artist.name.includes('&')) && req.params.and === 0){
console.log("& detected")
songNum++;
}
req.session.artist = searches[songNum].artist;
req.session.songArray = await req.session.artist.songs({sort: 'popularity', perPage: 20, page: 1});
req.session.currPage = 2
console.log(req.session.songArray.length)
res.status(200).json( { num: 20 } )
}
app.get('/artist/:artist/:and', initSongArray)
C:\Users\bgrie\Desktop\a\compsci.websites\typesiteNode\node_modules\express-session\index.js:598 var str = JSON.stringify(sess, function (key, val) { ^
TypeError: Converting circular structure to JSON --> starting at object with constructor 'Client' | property 'songs' -> object with constructor 'SongsClient' --- property 'client' closes the circle at JSON.stringify () at hash (C:\Users\bgrie\Desktop\a\compsci.websites\typesiteNode\node_modules\express-session\index.js:598:18) at isModified (C:\Users\bgrie\Desktop\a\compsci.websites\typesiteNode\node_modules\express-session\index.js:426:57) at shouldSave (C:\Users\bgrie\Desktop\a\compsci.websites\typesiteNode\node_modules\express-session\index.js:448:11) at ServerResponse.end (C:\Users\bgrie\Desktop\a\compsci.websites\typesiteNode\node_modules\express-session\index.js:334:11) at ServerResponse.send (C:\Users\bgrie\Desktop\a\compsci.websites\typesiteNode\node_modules\express\lib\response.js:232:10) at ServerResponse.json (C:\Users\bgrie\Desktop\a\compsci.websites\typesiteNode\node_modules\express\lib\response.js:278:15) at initSongArray (C:\Users\bgrie\Desktop\a\compsci.websites\typesiteNode\index.js:70:21) at processTicksAndRejections (node:internal/process/task_queues:96:5)