JMPerez / spotify-web-api-js

A client-side JS wrapper for the Spotify Web API
https://jmperezperez.com/spotify-web-api-js/
MIT License
1.86k stars 260 forks source link

Making Multiple Instances of the Wrapper #216

Open parsasaeedi opened 2 years ago

parsasaeedi commented 2 years ago

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 commented 2 years ago

https://stackoverflow.com/questions/73430871/making-multiple-instances-of-spotify-web-api-js

JMPerez commented 2 years ago

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