'Get value from each table row

I want to get the listing id from the table row when I press the button on that specific row.

So how can I read the value of listing.id when I press followItem?

I am able to get the userID from localstorage but I can’t get the listing.id when I press the button.

I get this error, which is at the listing.id:

allListings.js:82 Uncaught ReferenceError: res is not defined
   at followItem (allListings.js:82:19)
findAllListings.addEventListener("click", async () => {
    
    let maxPriceInput = document.getElementById("price")
    let maxPrice = maxPriceInput.value
    let maxDateInput = document.getElementById("date")
    let maxDate = maxDateInput.value
    
    let data = {
    maxPrice,
    maxDate
    };
    
      
    
      table.innerHTML = "";
      table.innerHTML += `
                <tr>
                    <th>picture</th>
                    <th>name</th>
                    <th>category</th>
                    <th>price</th>
                </tr>
                `;
    
                
      await fetch("http://localhost:4000/allListings", {
        method: "PUT",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(data),
      })
    
        .then((res) => res.json())
        .then((res) => {
          console.log(data)
          for (const key in res) {
            if (output == res[key].category) {
              table.innerHTML += `
                  <tr>
                      <td><img src="${res[key].picture}" style="height:200px;width:auto;"/></td>
                      <td>${res[key].name}</td>
                      <td>${res[key].category}</td>
                      <td>${res[key].price}</td>
                      <td>${res[key].listing_id}</td>
                      <td><input type="button" value="Show" class="knapper" onclick="followItem()" /></td>
                      </td>
    
    
                  </tr>
                  
    
                  `;
            }
          }
        })
        .catch((error) => {
          console.log(error);
        });
    });
    
    
    
    function followItem() {
      let userId = localStorage.getItem("loggedInUser");
      let listingid = res[key].listing_id;
    
      let data = {
        userId: userId,
        listingid: listingid
      
      };
    
      fetch("http://localhost:4000/allListings/follow", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(data),
      })
        .then((response) => response.json())
        .then((data) => {
          if (data.msg === true) {
            window.alert("you are now following this item");
            window.location = "./allListings.html";
          } else if (data.msg === false) {
            window.alert("Error, please try again");
          }
        })
        .catch((error) => {
          console.log(error);
        });
    };


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source