0

I'm trying to fetch data from an external JSON file into the inner HTML but I can't understand what the problem is. Data is smoothly displayed through the console but nothing is showing up on the HTML page.

Everything looks perfect :(

Please help me identify and resolve this issue

    <!DOCTYPE html>
    <html>
    <body>
        <div class="container">
        <table class="table table-stripped">
            <thead>
                <tr>
                    <th>Player ID</th>
                    <th>Player Name</th>
                    <th>Player Country</th>
                </tr>
            </thead>
            <tbody id="data">
            </tbody>
        </table>
        <script>
                fetch("http://localhost/test.json").then (
                    res=>
                    {
                        res.json().then
                        (
                            data=>
                            {
                                console.log(data);
                                if(data.length >0)
                                {
                                    var temp = "";

                                    //Beginning of the For loop

                                    data.forEach((u)=>{
                                        temp +="<tr>";
                                        temp +="<td>"+u.Id+"</td>";
                                        temp +="<td>"+u.Name+"</td>";
                                        temp +="<td>"+u.Country+"</td>"
                                        temp +=  "</tr>";
                                        })
                                    // --- End of the For Loop

                                    document.getElementById("data").innerHTML = temp; //The #tbody ID
                                }
                            }
                        )
                    }
                )
        </script>
    </body>
    </html>
2
  • Are you trying to display it as plain text? What is your test.json file? Commented Feb 5, 2020 at 15:15
  • Duplicate of: stackoverflow.com/questions/48292679/… Commented Feb 5, 2020 at 15:16

1 Answer 1

0

Javascript objects do not have length property. That value will be undefined, hence your conditional will never run.

Instead you can do:

if (Object.keys(data).length > 0)

If you want to ensure it doesn't die when there is no data (i.e. undefined), you can do:

if (typeof(data) === 'object' && Object.keys(data).length > 0)
Sign up to request clarification or add additional context in comments.

3 Comments

maybe script is running before DOMContentLodaded and there is no document.getElementById("data")
In both cases, I got the exact error: Uncaught (in promise) TypeError: data.forEach is not a function.
I tried replacing console.log(data); with console.log(JSON.stringify(data)); And all JSON records were displayed instead of 3 records only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.