JMPerez / spotify-web-api-js

A client-side JS wrapper for the Spotify Web API

Home Page:https://jmperezperez.com/spotify-web-api-js/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Making Multiple Instances of the Wrapper

parsasaeedi opened this issue · comments

commented

I'm creating an app where I need 2 instances of the wrapper, one for each user/access-token. But when I make 2 instances, it seems like they are both the same instance.

let SpotifyWebApi = require('spotify-web-api-js');
let spotifyApi1 = new SpotifyWebApi();
let spotifyApi2 = new SpotifyWebApi();

console.log(spotifyApi1.getAccessToken());
console.log(spotifyApi2.getAccessToken());

spotifyApi1.setAccessToken("Hello");
spotifyApi2.setAccessToken("Bye");

console.log(spotifyApi1.getAccessToken());
console.log(spotifyApi2.getAccessToken());

prints:

null
null
Bye
Bye

Where I expected it to print:

null
null
Hello
Bye

The access token in both instances is set to the latest set statement.

Same thing even if I write:

let SpotifyWebApi1 = require('spotify-web-api-js');
let SpotifyWebApi2 = require('spotify-web-api-js');
let spotifyApi1 = new SpotifyWebApi1();
let spotifyApi2 = new SpotifyWebApi2();

I don't know if this is a problem with the wrapper, or my lacking JavaScript skills. But I can't find a generic JavaScript answer on StackOverflow.

Additional context
I'm making a react app and I installed the wrapper package using npm.
Here is my project. I instantiate the wrappers in /src/Interspot.js and use them in /src/useSpotifyAPI.js

@parsasaeedi Yes, this looks definitely like a problem as your use case should be supported. This is a bug that should be addressed.