I have this function and is working fine:
const playlistPath = path.join(config.PLAYLIST_PATH, this.playlist.id + '.json')
let playlistString = JSON.stringify(this.playlist)
mkdirp(config.PLAYLIST_PATH, function (_) {
fs.writeFile(playlistPath, playlistString, function (err) {
if (err) return console.log('error saving album to playlist %s: %o', playlistPath, err)
console.log('The album has been added to the playlist');
})
})
But if I want to delete the var playlistString and use the JSON.stringify directly in is not working, the file is written with an undefined.
const playlistPath = path.join(config.PLAYLIST_PATH, this.playlist.id + '.json')
mkdirp(config.PLAYLIST_PATH, function (_) {
fs.writeFile(playlistPath, JSON.stringify(this.playlist), function (err) {
if (err) return console.log('error saving album to playlist %s: %o', playlistPath, err)
console.log('The album has been added to the playlist');
})
})
why ?