Troubleshooting user input for API scraping program












0















This program is supposed to output lyrics from a given API. This works when the song and artist are manually set at variables before pressing the submit button. However, I realize that it cannot work when taking user input for song because the JSON load is inside setup, which is loaded before the input is even given. How would I go about fixing this? I already tried to create another function for loadJSON, but it still was not working.



The expected output should be when you press submit, the user's song (I will deal with artist input later) lyrics should be produced. Thank you!



var search, button, question, input;
var artist = 'One%20Direction/';
var song = 'Taken';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'

function setup() {
noCanvas();

input = createInput ();
input.position (20, 50);

button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);

var url = url_root + artist + song;
loadJSON (url, gotData);

question = createElement ('h2', 'Search a song:');
question.position(20, 5);

}

function gotData(data) {
search = data;
}

function goSearch() {
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}









share|improve this question























  • your not showing the code for loadJson, and its not clear what your asking, perhaps add some more detail

    – saj
    Jan 1 at 2:58
















0















This program is supposed to output lyrics from a given API. This works when the song and artist are manually set at variables before pressing the submit button. However, I realize that it cannot work when taking user input for song because the JSON load is inside setup, which is loaded before the input is even given. How would I go about fixing this? I already tried to create another function for loadJSON, but it still was not working.



The expected output should be when you press submit, the user's song (I will deal with artist input later) lyrics should be produced. Thank you!



var search, button, question, input;
var artist = 'One%20Direction/';
var song = 'Taken';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'

function setup() {
noCanvas();

input = createInput ();
input.position (20, 50);

button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);

var url = url_root + artist + song;
loadJSON (url, gotData);

question = createElement ('h2', 'Search a song:');
question.position(20, 5);

}

function gotData(data) {
search = data;
}

function goSearch() {
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}









share|improve this question























  • your not showing the code for loadJson, and its not clear what your asking, perhaps add some more detail

    – saj
    Jan 1 at 2:58














0












0








0








This program is supposed to output lyrics from a given API. This works when the song and artist are manually set at variables before pressing the submit button. However, I realize that it cannot work when taking user input for song because the JSON load is inside setup, which is loaded before the input is even given. How would I go about fixing this? I already tried to create another function for loadJSON, but it still was not working.



The expected output should be when you press submit, the user's song (I will deal with artist input later) lyrics should be produced. Thank you!



var search, button, question, input;
var artist = 'One%20Direction/';
var song = 'Taken';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'

function setup() {
noCanvas();

input = createInput ();
input.position (20, 50);

button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);

var url = url_root + artist + song;
loadJSON (url, gotData);

question = createElement ('h2', 'Search a song:');
question.position(20, 5);

}

function gotData(data) {
search = data;
}

function goSearch() {
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}









share|improve this question














This program is supposed to output lyrics from a given API. This works when the song and artist are manually set at variables before pressing the submit button. However, I realize that it cannot work when taking user input for song because the JSON load is inside setup, which is loaded before the input is even given. How would I go about fixing this? I already tried to create another function for loadJSON, but it still was not working.



The expected output should be when you press submit, the user's song (I will deal with artist input later) lyrics should be produced. Thank you!



var search, button, question, input;
var artist = 'One%20Direction/';
var song = 'Taken';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'

function setup() {
noCanvas();

input = createInput ();
input.position (20, 50);

button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);

var url = url_root + artist + song;
loadJSON (url, gotData);

question = createElement ('h2', 'Search a song:');
question.position(20, 5);

}

function gotData(data) {
search = data;
}

function goSearch() {
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}






javascript p5.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 2:07









lydanglydang

1




1













  • your not showing the code for loadJson, and its not clear what your asking, perhaps add some more detail

    – saj
    Jan 1 at 2:58



















  • your not showing the code for loadJson, and its not clear what your asking, perhaps add some more detail

    – saj
    Jan 1 at 2:58

















your not showing the code for loadJson, and its not clear what your asking, perhaps add some more detail

– saj
Jan 1 at 2:58





your not showing the code for loadJson, and its not clear what your asking, perhaps add some more detail

– saj
Jan 1 at 2:58












1 Answer
1






active

oldest

votes


















0














I believe the following provides what you're after. In summary:




  • Addition of input.value() to get user's input value

  • Minor refactoring of functions





var search, button, question, input;
var artist = 'One%20Direction/';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'

// Page setup
function setup() {
noCanvas();

input = createInput ();
input.position (20, 50);

button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);

question = createElement ('h2', 'Search a song:');
question.position(20, 5);
}

// When data is returned, lets display it
function gotData(data) {
search = data;
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}

// Perform request when user submits search term
function goSearch() {
var song = input.value(); // Get the form field's input value
var url = url_root + artist + song;
loadJSON (url, gotData);
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.js"></script>








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992629%2ftroubleshooting-user-input-for-api-scraping-program%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I believe the following provides what you're after. In summary:




    • Addition of input.value() to get user's input value

    • Minor refactoring of functions





    var search, button, question, input;
    var artist = 'One%20Direction/';
    var url_root = 'http://lyric-api.herokuapp.com/api/find/'

    // Page setup
    function setup() {
    noCanvas();

    input = createInput ();
    input.position (20, 50);

    button = createButton ("Submit");
    button.position(input.x + input.width, 50);
    button.mousePressed(goSearch);

    question = createElement ('h2', 'Search a song:');
    question.position(20, 5);
    }

    // When data is returned, lets display it
    function gotData(data) {
    search = data;
    var thing = search.lyric
    var result = createP(thing);
    result.position (20, 60);
    }

    // Perform request when user submits search term
    function goSearch() {
    var song = input.value(); // Get the form field's input value
    var url = url_root + artist + song;
    loadJSON (url, gotData);
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.js"></script>








    share|improve this answer




























      0














      I believe the following provides what you're after. In summary:




      • Addition of input.value() to get user's input value

      • Minor refactoring of functions





      var search, button, question, input;
      var artist = 'One%20Direction/';
      var url_root = 'http://lyric-api.herokuapp.com/api/find/'

      // Page setup
      function setup() {
      noCanvas();

      input = createInput ();
      input.position (20, 50);

      button = createButton ("Submit");
      button.position(input.x + input.width, 50);
      button.mousePressed(goSearch);

      question = createElement ('h2', 'Search a song:');
      question.position(20, 5);
      }

      // When data is returned, lets display it
      function gotData(data) {
      search = data;
      var thing = search.lyric
      var result = createP(thing);
      result.position (20, 60);
      }

      // Perform request when user submits search term
      function goSearch() {
      var song = input.value(); // Get the form field's input value
      var url = url_root + artist + song;
      loadJSON (url, gotData);
      }

      <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.js"></script>








      share|improve this answer


























        0












        0








        0







        I believe the following provides what you're after. In summary:




        • Addition of input.value() to get user's input value

        • Minor refactoring of functions





        var search, button, question, input;
        var artist = 'One%20Direction/';
        var url_root = 'http://lyric-api.herokuapp.com/api/find/'

        // Page setup
        function setup() {
        noCanvas();

        input = createInput ();
        input.position (20, 50);

        button = createButton ("Submit");
        button.position(input.x + input.width, 50);
        button.mousePressed(goSearch);

        question = createElement ('h2', 'Search a song:');
        question.position(20, 5);
        }

        // When data is returned, lets display it
        function gotData(data) {
        search = data;
        var thing = search.lyric
        var result = createP(thing);
        result.position (20, 60);
        }

        // Perform request when user submits search term
        function goSearch() {
        var song = input.value(); // Get the form field's input value
        var url = url_root + artist + song;
        loadJSON (url, gotData);
        }

        <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.js"></script>








        share|improve this answer













        I believe the following provides what you're after. In summary:




        • Addition of input.value() to get user's input value

        • Minor refactoring of functions





        var search, button, question, input;
        var artist = 'One%20Direction/';
        var url_root = 'http://lyric-api.herokuapp.com/api/find/'

        // Page setup
        function setup() {
        noCanvas();

        input = createInput ();
        input.position (20, 50);

        button = createButton ("Submit");
        button.position(input.x + input.width, 50);
        button.mousePressed(goSearch);

        question = createElement ('h2', 'Search a song:');
        question.position(20, 5);
        }

        // When data is returned, lets display it
        function gotData(data) {
        search = data;
        var thing = search.lyric
        var result = createP(thing);
        result.position (20, 60);
        }

        // Perform request when user submits search term
        function goSearch() {
        var song = input.value(); // Get the form field's input value
        var url = url_root + artist + song;
        loadJSON (url, gotData);
        }

        <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.js"></script>








        var search, button, question, input;
        var artist = 'One%20Direction/';
        var url_root = 'http://lyric-api.herokuapp.com/api/find/'

        // Page setup
        function setup() {
        noCanvas();

        input = createInput ();
        input.position (20, 50);

        button = createButton ("Submit");
        button.position(input.x + input.width, 50);
        button.mousePressed(goSearch);

        question = createElement ('h2', 'Search a song:');
        question.position(20, 5);
        }

        // When data is returned, lets display it
        function gotData(data) {
        search = data;
        var thing = search.lyric
        var result = createP(thing);
        result.position (20, 60);
        }

        // Perform request when user submits search term
        function goSearch() {
        var song = input.value(); // Get the form field's input value
        var url = url_root + artist + song;
        loadJSON (url, gotData);
        }

        <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.js"></script>





        var search, button, question, input;
        var artist = 'One%20Direction/';
        var url_root = 'http://lyric-api.herokuapp.com/api/find/'

        // Page setup
        function setup() {
        noCanvas();

        input = createInput ();
        input.position (20, 50);

        button = createButton ("Submit");
        button.position(input.x + input.width, 50);
        button.mousePressed(goSearch);

        question = createElement ('h2', 'Search a song:');
        question.position(20, 5);
        }

        // When data is returned, lets display it
        function gotData(data) {
        search = data;
        var thing = search.lyric
        var result = createP(thing);
        result.position (20, 60);
        }

        // Perform request when user submits search term
        function goSearch() {
        var song = input.value(); // Get the form field's input value
        var url = url_root + artist + song;
        loadJSON (url, gotData);
        }

        <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.js"></script>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 14:20









        almcdalmcd

        93121325




        93121325
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992629%2ftroubleshooting-user-input-for-api-scraping-program%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas