Javascript 无法在Ionic服务中运行angular spotify方法

Javascript 无法在Ionic服务中运行angular spotify方法,javascript,angularjs,ionic-framework,spotify,Javascript,Angularjs,Ionic Framework,Spotify,我整天都在为在Ionic服务中处理Spotify数据的检索而苦苦挣扎,而之前的工作现在突然不起作用了 我已经在服务中设置了登录脚本,一切正常。但是,在console.log('updateInfo()调用')之后方法在updateInfo中,程序将跳过函数的其余部分,避免从Spotify收集数据 这是目前为止的全部服务: .factory('SpotifyService', function ($cordovaOauth, Spotify) { var currentUser = {},

我整天都在为在Ionic服务中处理Spotify数据的检索而苦苦挣扎,而之前的工作现在突然不起作用了

我已经在服务中设置了登录脚本,一切正常。但是,在
console.log('updateInfo()调用')之后方法在
updateInfo
中,程序将跳过函数的其余部分,避免从Spotify收集数据

这是目前为止的全部服务:

.factory('SpotifyService', function ($cordovaOauth, Spotify) {

  var currentUser = {},
        userId = '';

    function login() {
        console.log('login() called');
        $cordovaOauth.spotify('583ac6ce144e4fcb9ae9c29bf9cad5ef', ['user-read-private', 'playlist-read-private']).then(function (result) {
            window.localStorage.setItem('spotify-token', result.access_token);
            Spotify.setAuthToken(result.access_token);
        }, function (error) {
          console.log("Error ->" + error);
        });
    }

    function updateInfo() {
        console.log('updateInfo() called');
         Spotify.getCurrentUser().then(function (data) {
            console.log('Gathering data');

            currentUser = data;
            userId = data.id;

            console.log("CURRENT USER: " + userId + " - (" + currentUser + ")");
            console.log('Done updating');
        });
    }

    return {
        getUser: function () {
            console.log('SpotifyService.getUser() called');
            var storedToken = window.localStorage.getItem('spotify-token');
            if (storedToken !== null) {
                console.log('Token accepted');
                currentUser = updateInfo();
                console.log('getUser function returns: ' + userId + " - (" + currentUser + ")");
                //currentUser = Spotify.getCurrentUser();
            } else {
                console.log('getUser else accessed');
                login();
            }
            return currentUser;
        }
    };
})

你知道为什么Spotify.getCurrentUser()方法不能运行吗?

看来你的问题在于承诺。如果
Spotify.getCurrentUser()
返回一个承诺,您将不知道错误是什么,因为您尚未定义
catch
语句。你应该阅读一个简单明了的承诺解释

修改代码以添加catch语句:

Spotify.getCurrentUser()
.then(function (data) {
    ...
})
.catch(function(error){
    console.log(error);
});

看来你的问题在于承诺。如果
Spotify.getCurrentUser()
返回一个承诺,您将不知道错误是什么,因为您尚未定义
catch
语句。你应该阅读一个简单明了的承诺解释

修改代码以添加catch语句:

Spotify.getCurrentUser()
.then(function (data) {
    ...
})
.catch(function(error){
    console.log(error);
});

每次使用$cordovoauth.spotify()或spotify.getCurrentUser()之类的承诺时,都必须定义.catch块,这将有助于调试

将.catch块添加到getCurrentUser()函数调用以跟踪错误,我还建议将auth函数调用中的错误回调模式更改为.catch块

.catch(function(error){
  console.log(error);
});

每次使用$cordovoauth.spotify()或spotify.getCurrentUser()之类的承诺时,都必须定义.catch块,这将有助于调试

将.catch块添加到getCurrentUser()函数调用以跟踪错误,我还建议将auth函数调用中的错误回调模式更改为.catch块

.catch(function(error){
  console.log(error);
});

非常有帮助,谢谢。事实证明,经过几天的正确身份验证后,我从某处获得了错误的访问令牌:/:)很高兴我能提供帮助。当我开始使用承诺时,我遇到了同样的问题。非常有帮助,谢谢。事实证明,经过几天的正确身份验证后,我从某处获得了错误的访问令牌:/:)很高兴我能提供帮助。当我开始使用promises时,我遇到了同样的问题。你能播放Spotify曲目超过30秒吗?当时,我使用的是Spotify的Web API,它不允许完整的曲目播放。他们的开发者网站上列出了一些方法,可以帮助您为经过身份验证的用户设置完整的Spotify播放器,这些用户需要本地应用程序代码而不是web应用程序来运行。您能够播放Spotify track超过30秒吗?当时,我使用的是Spotify的web API,它不允许完整的曲目播放。他们的开发者网站上列出了一些方法,可以帮助您为经过身份验证的用户设置完整的Spotify播放器,这些用户需要本机应用程序代码而不是web应用程序才能运行。