【问题标题】:TypeError: Cannot read properties of undefined (reading 'map'TypeError:无法读取未定义的属性(读取“地图”
【发布时间】:2022-01-14 08:46:42
【问题描述】:

我试图让搜索结果显示在我的 html 页面上,并最终显示在一个表单中。问题是我的 .map 函数,当我运行代码时,我只能在控制台中看到我正在搜索的记录。 有什么建议? 我正在使用 nodejs、express 和 html。

 //Get sop number by combining url with data from input box
    getInfo();
    function getInfo() {
      document.getElementById("form");
      button.onclick = function (e) {
        e.preventDefault();
        const url = "http://localhost:6600/api/sopId";
        let sopSearch = document.getElementById("sop");
        fetch(`${url}/${sopSearch.value}`, {
          method: "GET",
        })
          .then((response) => {
            console.log(response);
            if (!response.ok) {
              throw Error("ERROR");
            }
            return response.json();
          })
          .then((data) => {
            console.log(data);
            const html = data.data.map((receipts) => {
              return `<p>SopNumber ${receipts.SOPNUMBE}</p>`;
            });
            console.log(html);
            document
              .querySelector("#app")
              .insertAdjacentElement("afterbegin", `<h1`);
          })
          .catch((error) => {
            console.log(error);
          });
      };
    }

【问题讨论】:

    标签: node.js promise chaining


    【解决方案1】:
    console.log(data)
    
    const html = data.data.map((receipts) => {
                  return `<p>SopNumber ${receipts.SOPNUMBE}</p>`;
                });
    

    请先控制台数据,然后检查是否有类似 data.data 的东西,以及 data.data 是否是一个数组,因为你正在映射到它

    希望你能得到答案

    【讨论】:

    • 好的,谢谢您的关心。我将开始评论而不是回答
    • 不不完全,数据已经显示在控制台上,我希望数据显示在我的html页面上。
    猜你喜欢
    • 2021-11-17
    • 2022-01-12
    • 2021-11-09
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    相关资源
    最近更新 更多