[英]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);
}
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.