Home > OS >  Insert/Add Data to Mysql from REST API with nodejs
Insert/Add Data to Mysql from REST API with nodejs

Time:10-21

Is there a way to insert the information i get from the API to MYSQL? i been looking everywhere and can't find nothing about it, google,youtube.... i'm going crazy. This is my code to fetch the data from the api.

from this code i get a ranking with Position, Name, Highscore.

NOTE: I'm not asking for someone to take time out of his day and make me the code, i'm simply asking for directions or a website that teaches you how to do it. THANK YOU IN ADVANCE.

getData();


async function getData() {
    const response = await fetch('http://localhost:3008/api/highscore/players')
    console.log(response);
    const data = await response.json();
    const req = await fetch('http://localhost:3008/api/players')
    console.log(req);
    const info = await req.json();
    length = data.players.length;
    console.log(data);
    var temp = "";
    for (i = 0; i < length; i  )
        for (j = 0; j < length; j  ) {
            {


                if (info.players[i].id == data.players[j].id) {

                    temp  = "<tr>";
                    temp  = "<td>"   data.players[j].position   "</td>";
                    temp  = '<td><a href="#">'   info.players[i].name   '</a></td>'
                    temp  = "<td>"   data.players[j].score   "</td>";
                    temp  = "</tr>";
                }

            }
        }
    document.getElementById("data").innerHTML = temp;
}


function myFunction() {
    var input, filter, table, tr, td, i, txtValue;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    table = document.getElementById("data");
    tr = table.getElementsByTagName("tr");
    for (i = 0; i < tr.length; i  ) {
        td = tr[i].getElementsByTagName("td")[1];
        if (td) {
            txtValue = td.textContent || td.innerText;
            if (txtValue.toUpperCase().indexOf(filter) > -1) {
                tr[i].style.display = "";
            } else {
                tr[i].style.display = "none";
            }
        }
    }
}```

CodePudding user response:

Looking at your code, I can see that it runs in a web browser, so it is a front end code that runs as a client.

First you need back-end code that runs on server side to connect to a MySQL server and run SQL queries. You can use node.js, PHP, Java etc. for this.

Usually, there would be APIs that also run on server side. The purpose of the API is to bridge between clients (web browsers, mobile apps etc.) and the back-end code that perform database queries.

After you have back-end and APIs, then in your front end code (like the one you included in the question), you would call an API endpoint (for example, http://localhost/api/insertTopScorer) and provide the required parameters, when you want to insert new data to database, or read data from database.

You can also learn more details here Can JavaScript connect with MySQL?

  • Related