Receiving TypeError: is undefined

时间:2015-07-28 23:01:18

标签: javascript json typeerror youtube-data-api

I'm getting a TypeError: liveVideo.items in undefined on line 59 of the following:

/* Youtube Live Embed
 * Copyright (C) 2015  Jerod Lycett
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

window.onload = embed;
function embed() {
    var apikey = "SANITIZED"; //Set to your API Key
    // See NOTEA below
    var channelName = "ThisWeekInScience"; //Set to your channel name
    var channelID;
    nameToID(channelName,apikey,function() {
        channelID = this;
    });
    //NOTEA: If you know it you can of course just comment that out and set the id manually
    var video;
    checkLive(channelID,apikey,function() {
        video = this;
    });
    var autoplay = "1";
    if(video.length == 0) {
        archiveVideo(channelID,apikey,function() {
            video = this;
        });
        autoplay = "0";
    }
    var embedCode = "<object height=\"350\" width=\"425\"><param name=\"movie\" value=\"https://www.youtube.com/v/" + video + "&autoplay=" + autoplay + "\"><embed height=\"350\" width=\"425\" type=\"application/x-shockwave-flash\" src=\"https://www.youtube.com/v/" + video + "&autoplay=" + autoplay + "\"></embed></object>";
    document.getElementById("liveembed").innerHTML = embedCode;
}
function nameToID(channelName,apikey,callback) {
    var getChannelID = new XMLHttpRequest();
    getChannelID.onreadystatechange = function() {
        if(getChannelID.readyState === 4) {
            var channelIDobj = JSON.parse(getChannelID.responseText);
            callback.call(channelIDobj.items[0].id);
        }
    }
    getChannelID.open("GET","https://www.googleapis.com/youtube/v3/channels?part=id&forUsername=" + channelName + "&fields=items%2Fid&key=" + apikey,true);
    getChannelID.send();
}
function checkLive(channelID,apikey,callback) {
    // In future check if soon live and refresh until live
    var getLive = new XMLHttpRequest();
    getLive.onreadystatechange = function() {
        if(getLive.readyState === 4) {
            var liveVideo = JSON.parse(getLive.responseText);
            if(liveVideo.items[0] === undefined) {
                callback.call("");
            }
            else {
                callback.call(liveVideo.items[0].id.videoId);
            }
        }
    }
    getLive.open("GET","https://www.googleapis.com/youtube/v3/search?part=id&channelId=" + channelID + "&eventType=live&maxResults=1&type=video&fields=items%2Fid&key=" + apikey,true);
    getLive.send();
}
function archiveVideo(channelID,apikey,callback) {
    var getPlaylist = new XMLHttpRequest();
    getPlaylist.onreadystatechange = function() {
        if(getPlaylist.readyState === 4) {
            var playlistObj = JSON.parse(getPlaylist.responseText);
            var playlistID = playlistObj.items[0].contentDetails.relatedPlaylists.uploads;
            var getArchiveVideo = new XMLHttpRequest();
            getArchiveVideo.onreadystatechange = function() {
                if(getArchiveVideo.readyState === 4) {
                    var archiveVideoObj = JSON.parse(getArchiveVideo.responseText);
                    callback.call(archiveVideoObj.items[0].contentDetails.videoId);
                }
            }
            getArchiveVideo.open("GET","https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=1&playlistId=" + playlistID + "&fields=items%2FcontentDetails&key=" + apikey,true);
            getArchiveVideo.send();

        }
    }
    getPlaylist.open("GET","https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=" + channelID + "&fields=items%2FcontentDetails&key=" + apikey,true);
    getPlaylist.send();
}

It throws another one with video, but I think that's because of the first TypeError. However line 59 is specifically checking if liveVideo.items is undefined. The JSON being parsed is

{
 "items": [
 ]
}

It also returns http 200 for that, so I can't use anything like that to check, I'm basically parsing blindly to find out if there is or isn't anything.

1 个答案:

答案 0 :(得分:0)

I've gotten it to work by rewriting parts of it, especially by removing the part of it that checks for the ID.

parent = self.parent

If you have a better idea, let me know.