15

I'm working on a Spotify app. I'm able to login and get my token. My problem is I cannot access a variable outside the method. In this case "getCurrentUser"

This is my method:

function getUser() {
  if ($localStorage.token == undefined) {
    throw alert("Not logged in");
  } else {
    Spotify.getCurrentUser().then(function(data) {
      var names = JSON.stringify(data.data.display_name);
      console.log(names)
    })
  }
};

As you can see I console.logged the name and I do get the right value in the console. But only works there if I call the function getUser() I get undefined even with a return of the names variable.

I need to $scope that variable.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Rodolfo R
  • 397
  • 1
  • 3
  • 17
  • I'm guessing you have `return names`, but you also need `return Spotify.getCurrentUser()...` so that it returns the promise (and ultimately the return value from the then) – Danny Nov 29 '17 at 18:59

2 Answers2

9

getUser() is not returning anything. You need to return the promise from the Spotify.getCurrentUser(), and then when you return names within that it is returned by the outer function.

function getUser() {

    if ( $localStorage.token == undefined) {
        throw alert("Not logged in");
    }
    else {
        return Spotify.getCurrentUser().then(function(data) {
            var names = JSON.stringify(data.data.display_name);
            console.log(names)
            return names;
        })
    }
}

The above answered why you were getting undefined when calling getUser(), but if you want to work with the end result you also want to change how you're using the value you get from getUser - it returns a promise object, not the end result you're after, so your code wants to call the promise's then method when the promise gets resolved:

getUser()                        // this returns a promise...
   .then(function(names) {       // `names` is the value resolved by the promise...
      $scope.names = names;      // and you can now add it to your $scope
   });
Danny
  • 1,740
  • 3
  • 22
  • 32
  • 1
    Sorry I'm new in this, It worked but I get a object that starts with $$state how do I access the data it contains? – Rodolfo R Nov 29 '17 at 19:46
  • Could you help me with that? – Rodolfo R Nov 29 '17 at 20:10
  • I added some code to handle the promise you get from `getUsers()` - I think that is a lot cleaner than the other answer I linked to in a comment earlier. – Danny Nov 29 '17 at 20:45
  • So the function inside `then` will return a value that will then be returned by `getUser`? – Melab May 30 '19 at 21:21
  • @Melab yes - `getUser()` returns a promise. You can access its final resolved value in the `then` function of that promise – Danny May 30 '19 at 22:14
0

If you use it like that you can use the await call

function getUser() {

    if ( $localStorage.token == undefined) {
        throw alert("Not logged in");
    }
    else {
        return Spotify.getCurrentUser().then(function(data) {
            var names = JSON.stringify(data.data.display_name);
            console.log(names)
            return names;
        });
    }
}

const names = await getUser();
wadie
  • 496
  • 1
  • 10
  • 24
Jorge Monroy
  • 408
  • 1
  • 5
  • 16
  • 2
    Can we use await without adding async in function deceleration :? – Saeed Ansari Jun 24 '20 at 05:58
  • 2
    No, you can't use await without async, If you will try to use you will get an error for the same. – Chandni Jan 09 '21 at 02:47
  • @SaeedAnsari Yes, of course. `await` works with Promises. `async` simply exposes a Promise as the return value. If you already return a Promise then you can `await` it without `async` – BLewis Mar 14 '22 at 09:53