0

How would I access buildLoginUrl() in my call to login()? The first here isn't being printed, and I think it's because the call to login() is never returned.

main.js:

$(document).ready(function() {
    // login function to kick off the authorization
    function login(callback) {
        console.log("here2");
        // builds the login URL for user after clicking login button
        function buildLoginUrl(scopes) {
            console.log("here3");
            return 'https://accounts.spotify.com/authorize?client_id=' + clientID +
              '&redirect_uri=' + redirectURI +
              '&scope=' + scopes +
              '&response_type=token';
        }

        // other stuff
    }
// event listeners
    $("#login").click(function() {
        // call the login function, we'll get back the accessToken from it
        console.log("here1");
        login(function(accessToken) {
            // callback function from login, gives us the accessToken

            //buildLoginUrl(scopes);
            var request = getUserData(accessToken);
            console.log("here");
            request.get(options, function(error, response, body) {
                console.log(body);
            });
            // other stuff
        });
     });

Console:

here1
here2
here4
8
  • 1
    You are not calling buildLoginUrl anywhere in your example. Where exactly do you want to call it? Commented Apr 3, 2017 at 21:27
  • You can't access it from outside the enclosing function unless you assign it to an accessible (e.g. global) variable or return it from the function. Why don't you declare it directly in the anonymous function? Commented Apr 3, 2017 at 21:28
  • buildLoginUrl() is never called so here3 will never show. Commented Apr 3, 2017 at 21:35
  • Your login() function never calls the callback function and that's why here is never logged. Commented Apr 3, 2017 at 21:36
  • I can't change any of the code in login() because that was given to me for this problem, but I can edit the code within login(function(accessToken)). I don't think I have to call buildLoginUrl, but I'm not sure why here isn't printed to the console Commented Apr 3, 2017 at 21:41

2 Answers 2

1

You are passing a callback to login, but the login function never calls the callback. That's why you'd never see "here"

Sign up to request clarification or add additional context in comments.

2 Comments

How would I be able to have the console print out the first here? There here between var request and request.get()
Call the callback anywhere inside of login. For example: function login(callback) { callback(); }
0

The first here isn't being printed, and I think it's because the call to login() is never returned.

To keep it simple:

// here yo are defining login()
function login(callback) {
    callback();
}

// here you are invoking login()
login(function() {
    console.log('here');
});

here doesn't get printed because you never invoked the callback within login(); you have to call callback as shown above in order for here to be printed.

2 Comments

After reading these comments, I added buildLoginUrl(scopes); to the end of function login(callback), but here is stil not printed, but here 1-4 are printed
You have to invoke the callback first before anything else for here to print.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.