thelinmichael / spotify-web-api-node

A Node.js wrapper for Spotify's Web API.
http://thelinmichael.github.io/spotify-web-api-node/
MIT License
3.11k stars 499 forks source link

Get playlists that were created by the current user #348

Open ghost opened 3 years ago

ghost commented 3 years ago

Although .getUserPlaylists() returns the current user's playlists, I am trying to fetch only the ones created by them. This means that playlists they are following (e.g. ones created by spotify) will not be returned. So far I'm working on a solution using jsonpath but is there an easier way to retrieve them, without using other packages (nor databases) ?

ghost commented 3 years ago

This is my code for now, which seems to work fine:

app.route('/createdbyuser') 
   .get(function(req, res) {

    spotifyApi.getMe()
    .then(function(data) {
      const userid = data.body.id;
      console.log('Authenticated user: ', userid);

      spotifyApi.getUserPlaylists()
      .then(function(data) {
        res.send('Check console for details.');
        const injson = JSON.stringify(data.body, null, 4);
        const currdata = JSON.parse(injson);
        let myplaylistlib = [];
        myplaylistlib.push( jp.query(currdata, '$.items[?(@.owner.id==userid)]') );

      },function(err) {
        console.log('Something went wrong!', err);
      });
    }, function(err) {
      console.log('Something went wrong!', err);
    });
});

Like I said, I'm using jsonpath but there must be a simpler way. Also, the nesting feels wrong. Open to suggestions to make it more optimal!