未捕获的类型错误:无法读取 AJAX 对象上未定义的属性“map”
2020-06-15
126
如果没有结果,我是否可以将 N/A 作为默认值传递?如果映射对象的标题为空,我希望能够将其定义为“N/A”。
Media_x0020_Specialist_x0028_s_x.results
下的某些项目返回,但有些项目没有返回,我收到了该错误。
代码:
$.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);
}
2个回答
您可以使用三元运算符并对每个项目重复此操作:
(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")
就我个人而言,我会将其括在括号中,以便更清楚地了解正在发生的事情(如上所示)。
或者,使用较新的 javascript 功能,您可以使用可选链接,它是为了解决此问题而创建的:
item[i].Franchises_x0020_with_x0020_Shar?.results
.map(({ Title }) => Title))
.join("; ")
|| "N/A"
Dylan Kerler
2020-06-15
进一步的建议:您那里有很多重复的代码,并且使用
+
在 javascript 中连接字符串的可读性不如使用
模板文字
。
这是一个更简洁、可读性更强的解决方案,并且可以重复用于您可能拥有的其他数据:
$.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);
}
simmer
2020-06-15