0

the code I am using without params / filling: https://www.npmjs.com/package/request

    var request = require('request');
    request('http://www.google.com', function (error, response, body) {
        console.log('error:', error); // Print the error if one occurred
        console.log('statusCode:', response && response.statusCode); 
        // Print the response status code if a response was received
        console.log('body:', body); // Print the HTML for the Google homepage.
    });

My code:

    const YTAPIVideoURL = ('https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=' + Channel_ID + '&eventType=live&type=video&key=' + YT_API_KEY);

    const YTAPIStatusURL = ('https://www.googleapis.com/youtube/v3/liveBroadcasts?part=id%2Csnippet%2Cstatus&mine=true&broadcastStatus=active&key='+YT_API_KEY);


    var livestreamStatus = true;
    if (livestreamStatus == true) {
        var VIDEO_ID = "";
        var VIDEO_TITLE = "";

    // request options
        const APIoptions = {
            url: YTAPIVideoURL,
            method: "GET",
            timeout: 10000,
            followRedirect: true,
            maxRedirects: 10
        };

     //Request
        const APIrequest = request(APIoptions,
        function(error, response, body) {
            if (!error && response.statusCode == 200) {
                var jsonContent = JSON.parse(body);
                console.log("jsonContent: " + jsonContent);
                console.log("parsed info: " + jsonContent["items"][0]["id"]["videoId"]);
                if ((jsonContent["items"][0]) != [""]) {
                    VIDEO_TITLE = jsonContent["items"][0]["snippet"]["title"];
                    VIDEO_ID = jsonContent["items"][0]["id"]["videoId"];

                    console.log(VIDEO_ID) //THIS IS THE DATA I NEED

                } else {
                    videoId = false;
                    VIDEO_TITLE = false;
                }
                /*console.log(videoId);*/
            } else {
                console.log('error' + response.statusCode);
            }
        });
        console.log(VIDEO_ID); //THIS IS WHERE I NEED THE DATA

I need to retrieve the VIDEO_ID from inside the require function... I've searched and am having a lot of trouble as all the solutions out there are just satisfied with using console.log() to print out the data from inside the require object|module..

The data that I want is grabbed and put in the var VIDEO_ID and displayed as it should on the lines; console.log("parsed info: " + jsonContent["items"][0]["id"] and console.log(VIDEO_ID) //THIS IS THE DATA I NEED Those lines log the data I need as it should.

I need to have VIDEO_ID accessible from outside the function here: console.log(VIDEO_ID); //THIS IS WHERE I NEED THE DATA I know there are functions like require.XXXX(APIoptions, callback) but I have no idea what the callback is, and which one I should use.. help pls?

Lezappen
  • 1
  • 2
  • Can you include the content of the `jsonContent` variable in your question? – NullDev Nov 26 '17 at 18:13
  • You're making an async call it seems. use a callback in your `if`. The `console.log(VIDEO_ID);` is called way to early. – kevinSpaceyIsKeyserSöze Nov 26 '17 at 18:18
  • jsonContent is an object ([object Object]) or the parsed json file, this isn't the source of the problem as the data I need is already in a variable and the string I need from that json is put in the VIDEO_ID var without any problems. I don't know what a callback is, or which one I should use? – Lezappen Nov 26 '17 at 18:26

0 Answers0