0

I'am trying to fetch data from a online MongoDb database using javascript and html. but it doesn't work properly. Also using jquery library too

<div>
    <input type="submit" value="GET DATA FROM API" id="getAPI">
    <div id="result" ></div>
    <div ><h2>Input Form</h2></div>
    <form id="postData" >
    </form>
</div>
<!-- javascript part -->
<script>
    document.getElementById('getAPI').addEventListener('click', getAPI);
    function getAPI() {
        fetch('http://localhost:3000/api/seller')
            .then((res) => { return res.json() })
            .then((data) => {
                let result = `<h2> Random User Info From Jsonplaceholder API</h2>`;
                data.forEach((seller) => {
                    const {id, name, email} = seller
                    result +=
                        `<div>
                                <h5> User ID: ${id} </h5>
                                <ul>
                                    <li> User Full Name : ${name}</li>
                                    <li> User Email : ${email} </li>
                                </ul>
                             </div>`;
                    document.getElementById('result').innerHTML = result;
                });
            })
    }
</script>

<script src="jquery-3.3.1.min.js" type="text/javascript"></script>
5
  • What part of the code doesn't work? Commented May 30, 2019 at 18:28
  • it doesn't give any result as a output Commented May 30, 2019 at 18:31
  • Does it make the ajax request? Is there a JSON response? Commented May 30, 2019 at 18:34
  • this can be seen in the chrome console view.html:1 Unchecked runtime.lastError: The message port closed before a response was received. view.html:1 Unchecked runtime.lastError: The message port closed before a response was received. Commented May 30, 2019 at 18:58
  • now this view.html?_ijt=72t9n4ls8vjcs5g7minkkcai31:28 Uncaught (in promise) TypeError: data.forEach is not a function Commented May 30, 2019 at 19:35

1 Answer 1

1

fetch("https://jsonplaceholder.typicode.com/users")
  .then(res => {
    return res.json();
  })
  .then(data => {
    let result = `<h2> Random User Info From Jsonplaceholder API</h2>`;
    data.forEach(seller => {
      const {
        id,
        name,
        email
      } = seller;
      result += `<div>
                   <h5> User ID: ${id} </h5>
                   <ul>
                     <li> User Full Name : ${name}</li>
                     <li> User Email : ${email} </li>
                   </ul>
                 </div>`;
      document.getElementById("app").innerHTML = result;
    });
  });
<div id="app"></div>

Your Front End works well. I replaced the api url in your code and everything is fine. It might be something on the Back End. Did you check the status code that you get from the server?

Sign up to request clarification or add additional context in comments.

11 Comments

This should be a comment, not an answer.
Yes, but I can't execute code example inside the comments and by the code example, there is nothing to change on the FE side.
I checked the api using postman and it worked fine. Is that okay ?
Im seeing thin in chrome console Uncaught SyntaxError: Unexpected token < 8Unchecked runtime.lastError: The message port closed before a response was received.
Can you open network tab in the chrome dev tools? Here you can check more info about response from the server.
|

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.