简体   繁体   English

未捕获的类型错误:无法读取 AJAX object 上未定义的属性“映射”

[英]Uncaught TypeError: Cannot read property 'map' of undefined on AJAX object

Is there a way that I can pass in N/A as default if there is no results?如果没有结果,有没有办法可以将 N/A 作为默认值传递? I want to be able to define it as "N/A" if the mapped object is empty on Title.如果映射的 object 在 Title 上为空,我希望能够将其定义为“N/A”。

Some of the items under Media_x0020_Specialist_x0028_s_x.results return, but then some items are not returning and I'm getting that error. Media_x0020_Specialist_x0028_s_x.results下的一些项目返回,但随后一些项目没有返回,我收到了该错误。

Code:代码:

  $.ajax({
    url: requestUri,
    type: "GET",
    dataType: "json",
    cache: false,
    headers: {
      accept: "application/json; odata=verbose"
    },
    success: function success(data) {
      onSuccess(data);
      ExportTable();
    }
  });

  function onSuccess(data) {
    var item = data.d.results;
    for (var i = 0; i < item.length; i++) {
      tableContent += "<tr>";
      tableContent +=
        "<td>" +
        item[i].Franchises_x0020_with_x0020_Shar.results
          .map(function(r) {
            return r.Title;
          })
          .join("; ") +
        "</td>";
      tableContent +=
        "<td>" + item[i].Stand_x0020_Alone_x0020_Franchis + "</td>";
      tableContent +=
        "<td>" +
        item[i].Franchise_x0020_Liason.results
          .map(function(r) {
            return r.Title;
          })
          .join("; ") +
        "</td>";


      tableContent +=
        "<td>" +
        item[i].Media_x0020_Specialist_x0028_s_x.results
          .map(function(r) {
            return r.Title;
          })
          .join("; ") +
        "</td>";

      tableContent += "</tr>";
      tableContent += "</tbody></thead>";
    }
    $("#title").append(tableContent);
  }

Here is the error:这是错误: 在此处输入图像描述

You can use a ternary operator and repeat this for each item:您可以使用三元运算符并对每个项目重复此操作:

(item[i].Franchises_x0020_with_x0020_Shar.results ?          
   item[i].Franchises_x0020_with_x0020_Shar.results
      .map(function(r) {
          return r.Title;
      })
      .join("; ")
:
   "N/A")

Personally, I would wrap it in parenthesis to make it clearer what is going on (like above).就个人而言,我会将其括在括号中以更清楚地说明发生了什么(如上)。

Alternatively, with the newer javascript features you can use optional chaining which was created to address this very problem:或者,使用更新的 javascript 功能,您可以使用为解决这个问题而创建的可选链接:

   item[i].Franchises_x0020_with_x0020_Shar?.results
          .map(({ Title }) => Title))
          .join("; ") 
   || "N/A"

Further advice: you've got a lot of repeated code there, and concatenating strings in javascript using + is not as readable as doing so with template literals .进一步的建议:那里有很多重复的代码,并且使用+连接 javascript 中的字符串不如使用模板文字那样可读。

Here's a solution that's more concise, readable, and can be repeated for other data you might have:这是一个更简洁、易读且可以重复用于您可能拥有的其他数据的解决方案:

$.ajax({
  url: requestUri,
  type: "GET",
  dataType: "json",
  cache: false,
  headers: {
    accept: "application/json; odata=verbose"
  },
  success: function success(data) {
    onSuccess(data);
    ExportTable();
  }
});


// we'll reuse this function each time we want to get a semicolon-separated list of titles
function getFranchiseTitles(franchises) {
  var noResultsMessage = 'N/A';

  // is "results" present on this object? does it have length?
  var hasResults = !!franchises.results && !!franchises.results.length;

  // only attempt map if there are results to map over
  return hasResults
    ? franchises.results.map(f => f.Title).join('; ')
    : noResultsMessage;
}


function onSuccess(data) {
  var results = data.d.results;

  // map over results, instead of using a for loop
  var tableRows = results.map(result => {

    // destructure & rename each result to get child results
    var {
      Franchises_x0020_with_x0020_Shar: shared,
      Stand_x0020_Alone_x0020_Franchis: standalone,
      Franchise_x0020_Liason: liason,
      Media_x0020_Specialist_x0028_s_x: specialist
    } = result;

    // get our titles from child objects
    var sharedTitles = getFranchiseTitles(shared);
    var standaloneTitles = getFranchiseTitles(standalone);
    var liasonTitles = getFranchiseTitles(liason);
    var specialistTitles = getFranchiseTitles(specialist);

    // return markup for this table row with titles inserted
    return `<tr>
      <td>${sharedTitles}</td>
      <td>${standaloneTitles}</td>
      <td>${liasonTitles}</td>
      <td>${specialistTitles}</td>
    </tr>`
  }).join('');

  // wrap with outer table tags
  var tableContent = `<table><tbody>${tableRows}</tbody></table>`;

  // insert into element
  $("#title").append(tableContent);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Uncaught typeError:无法读取未定义的属性-对象 - Uncaught typeError: Cannot read property of undefined - object 未捕获的类型错误:无法读取未定义的属性“对象” - Uncaught TypeError: Cannot read property 'object' of undefined 未捕获的TypeError:无法使用React读取未定义的属性“map” - Uncaught TypeError: Cannot read property 'map' of undefined with React 错误:未被捕获的TypeError:无法读取未定义的属性“映射” - Error: Uncaught TypeError: Cannot read property 'map' of undefined 未捕获到的TypeError:无法读取Google Map API中未定义的属性“ PlacesService” - Uncaught TypeError: Cannot read property 'PlacesService' of undefined in google map api 未捕获的TypeError:无法读取未定义的Google Map和jquery的属性&#39;x&#39; - Uncaught TypeError: Cannot read property 'x' of undefined Google Map and jquery 未捕获的TypeError:无法读取Highcharts的undefined属性“ map” - Uncaught TypeError: Cannot read property 'map' of undefined with Highcharts Axios 未捕获类型错误:无法读取未定义的属性“映射” - Axios Uncaught TypeError: Cannot read property 'map' of undefined 未捕获的TypeError:无法读取未定义的传单地图ArcIGS的属性“ lat” - Uncaught TypeError: Cannot read property 'lat' of undefined Leaflet map ArcIGS reactjs-未捕获的TypeError:无法读取未定义的属性&#39;map&#39; - reactjs - Uncaught TypeError: Cannot read property 'map' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM