Jammming (search results only appears on the 2nd search, WHY?)

Searching for a song on the first search renders no results, but on the second search renders the results, WHY?

I realized it has to do with access token, but can’t figure out the solution.

Any help would be appreciated.

So you figured out why npm start did not work for you? What was it?

Your access token on first search is not set yet. Given your getAccessToken function looks like mine you have an else clause for the case that neither your accessToken is set yet nor your current URL matches the RegEx for the accessToken to be set.
If you enter a search term for the first time, the search function is called. The search function calls the getAccessToken function. The ‘else’ in the getAccessToken function is entered and the statement sets the window location and returns false.
So this is what your search function looks like on first search:

const accessToken = false //(this.getAccessToken();)
 const headers = {'Authorization': `Bearer false`}

That behaviour is expected.

I left the issue with npm start to be addressed later with regards to these projects(Animal Fun Facts, Appointment Planner and Flashcards) so I haven’t attended to that yet.

With the Jammming project, I created the app from scratch on my local machine so I didn’t have any issues with npm start, it works fine for the Jammming project.

I now understand the issue at hand. Does it mean the ‘else’ condition is the problem? Have you tried refactoring the condition statement to get a desired result?

What was your solution?

I left it as it is. The only solution I can think of is a start / open button on a start screen that leads to the App’s main page and calls the getAccessToken and sets the URL and accessToken. But I haven’t done that.

Thanks for your contribution.

1 Like

Theoretically, could it work, in the else clause, returning a recursive call to the getAccessToken function?

@mirja_t and @kalinho
I have found a solution and it works fine for me.

I declared Spotify.getAccessToken(); in the App.js and it works for searching on the first attempt.

1 Like

This solution also worked for me.