-1

So i want to make a list of these orders and implement a search function to it but i cant even figure the basics like now it just spams undefined on the html even though the "orderid" is a object on the json file. this is really hard to figure out and when i ask people, people give too straight forward messages to me that i cant figure it im not asking you guys to code for me but to point to right direction would helpful

const orderlist = document.getElementById('orderlist');
let orderid = [0];

const loadorders = async() => {
  try {
    const res = await fetch('https://www.cc.puv.fi/~asa/cgi-bin/fetchOrders.py');
    orderid = await res.json();
    displayorderid(orderid);
  } catch (err) {
    console.error(err);
    console.log(res);
  }
};
const displayorderid = (orderid) => {
  const htmlString = orderid
    .map((order) => {
      return `
        <li class="orderid">
        <h2>${order.value}</h2>
        </li>
        `;
    })
    .join('');
  orderlist.innerHTML = htmlString;
};
loadorders();
body {
  font-family: Georgia, serif;
  background-color: rgb(59, 59, 243);
}

* {
  box-sizing: border-box;
}

h1 {
  color: azure margin-bottom:30px;
}

.container {
  padding: 35px;
  margin: 0 auto;
  max-width: 1000px;
  text-align: center center;
}

#customerslist {
  padding-inline-start: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  grid-gap: 15;
}

.Customer {
  list-style-type: none;
  background-color: aquamarine;
  border-radius: 5px;
  padding: 10px 25px;
  grid-template-columns: 3fr 1fr;
  grid-template-areas: ;
  text-align: left;
}
<div class="container">
  <h1>tilaukset</h1>
  <div id="search">
    <input type="text" name="Hakupalkki" id="Hakupalkki" placeholder="Hae tilausta" />
  </div>
  <ul id="orderlist"></ul>
</div>
Kinglish
  • 23,358
  • 3
  • 22
  • 43
Jesse
  • 1
  • 2
  • 1
    I turned your code into a snippet - note that this is the preferred way to present code as it allows you and others to test it right here in the question. The issue is there is no `value` property in the objects you are looping through. Examine the object by putting a `console.log(order)` in your `map` function. – Kinglish Nov 28 '22 at 21:18
  • What do you want to show exactly? – DreamBold Nov 28 '22 at 21:22
  • https://i.imgur.com/05au1Qf.png I have displayed the `orderid`s @Jesse – DreamBold Nov 28 '22 at 21:23
  • `

    ${orderid.orderid}

    `
    – DreamBold Nov 28 '22 at 21:29

2 Answers2

1

Here's a working snippet. As mentioned by Kinglish (in the comments), the issue is there is no value property in the objects you are looping through it should say customerid instead.

const orderlist = document.getElementById('orderlist');
let orderid = [0];

const loadorders = async() => {
  try {
    const res = await fetch('https://www.cc.puv.fi/~asa/cgi-bin/fetchOrders.py');
    orderid = await res.json();
    displayorderid(orderid);
  } catch (err) {
    console.error(err);
    console.log(res);
  }
};
const displayorderid = (orderObject) => {
  const htmlString = orderObject
.map((orderObject) => {
  return `
    <li class="orderid">
    <h2>${orderObject.customerid}</h2>
    </li>
    `; //<------- the customerid property exists
})
.join('');
  orderlist.innerHTML = htmlString;
};
loadorders();
body {
  font-family: Georgia, serif;
  background-color: rgb(59, 59, 243);
}

* {
  box-sizing: border-box;
}

h1 {
  color: azure margin-bottom:30px;
}

.container {
  padding: 35px;
  margin: 0 auto;
  max-width: 1000px;
  text-align: center center;
}

#customerslist {
  padding-inline-start: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  grid-gap: 15;
}

.Customer {
  list-style-type: none;
  background-color: aquamarine;
  border-radius: 5px;
  padding: 10px 25px;
  grid-template-columns: 3fr 1fr;
  grid-template-areas: ;
  text-align: left;
}
<div class="container">
  <h1>tilaukset</h1>
  <div id="search">
    <input type="text" name="Hakupalkki" id="Hakupalkki" placeholder="Hae tilausta" />
  </div>
  <ul id="orderlist"></ul>
</div>
Alicia Sykes
  • 5,997
  • 7
  • 36
  • 64
  • Also note that Lissy93 renamed the parameter `orderid` to `orderObject`, which is more understandable. Even better would be to somehow mediate that the object in fact is an array, e.g. by naming it `orderObjects` (notice the s) or just `orders`. – Oskar Grosser Nov 28 '22 at 21:28
0

There is no value property of each object in the array. Try this for example:

const orderlist = document.getElementById('orderlist');
let orderid = [0];

const displayorderid = (orderid) => {
  const htmlString = orderid
    .map((orderid) => {
      return `
        <li class="orderid">
        <h2>${orderid.customerid}</h2>
        </li>
        `; //<------- the customerid property exists
    })
    .join('');
  orderlist.innerHTML = htmlString;
};

const loadorders = async () => {
  try {
    const res = await fetch(
      'https://www.cc.puv.fi/~asa/cgi-bin/fetchOrders.py'
    );
    orderid = await res.json();
    console.log(orderid) //<------- use this to view the each object`s properties in the console
    displayorderid(orderid);
  } catch (err) {
    console.error(err);
    console.log(res);
  }
};

loadorders();

Hope this helps.

damonholden
  • 1,062
  • 4
  • 17