当我想要Spotify API上当前正在播放的曲目的音频功能时,如何摆脱出现的错误?

时间:2018-11-06 00:22:21

标签: javascript jquery spotify

所以我有这段代码,在这里我使用var来获取当前正在播放的歌曲的音频功能。我已经有了代码,但是出现此错误,提示

  

未捕获的TypeError:无法读取undefined *的属性'id'。

有人知道我在做什么错吗?请尽可能简单地解释一下,我是新手。

The error I see in my console

以下是文本代码:

setInterval(function(){
    callAjax();
}, 1000);

var apiData;
var audioData;

var callAjax = function(){

    $.ajax({
        url: 'https://api.spotify.com/v1/me/player/currently-playing',
        headers: {
          'Authorization': 'Bearer ' + access_token
        },
        success: function(response) {
          userCurrentlyPlaceholder.innerHTML = userCurrentlyTemplate(response);

          apiData = response;
          console.log(response);
          $('#login').hide();
          $('#loggedin').show();
        }
    });

    if (apiData = ! undefined){
        $.ajax({
            url: "https://api.spotify.com/v1/audio-features/" + apiData.item.id,
            headers: {
                'Authorization': 'Bearer ' + access_token
            },
            success: function(response) {
                userFeaturePlaceholder.innerHTML = userFeatureTemplate(response);

                audioData = response;
                console.log(response);
                $('#login').hide();
                $('#loggedin').show();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

第二次ajax调用之前,undefined-check似乎有问题。它可能应该显示为SELECT REPLACE(LTRIM(REPLACE(CardNo, '0', ' ')),' ', '0') AS staff_id, FORMAT(PunchDatetime, 'yyyy-MM-dd') as 'date', FORMAT(min(PunchDatetime), 'HH:mm:ss') AS time_in, IIF(min(PunchDatetime) != max(PunchDatetime), FORMAT(max(PunchDatetime), 'HH:mm:ss'), NULL) AS time_out FROM PunchRecords WHERE cast('2018-11-01' as date) = cast(FORMAT(PunchDatetime, 'yyyy-MM-dd') as date) group by CardNo, FORMAT(PunchDatetime, 'yyyy-MM-dd') order by CardNo"; ,但是您拥有的是apiData != undefined。这有效地将apiData = ! undefined设置为apiData,其评估结果为true。假设您成功检索了!undefined,则该数据将丢失,并且随后进行的任何访问尝试都将失败。

apiData

很可能是由于未经授权的访问而导致第一个ajax调用失败,并且apiData始终未定义。