Javascript 未捕获的TypeError:为空
我正在使用ajax在页面中获取表格格式。问题是它正在工作,目前我遇到了这个问题 我的代码 错误 未捕获的TypeError:无法读取null的属性“slug”Javascript 未捕获的TypeError:为空,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在使用ajax在页面中获取表格格式。问题是它正在工作,目前我遇到了这个问题 我的代码 错误 未捕获的TypeError:无法读取null的属性“slug” 您正在使用for each,因此我想您将在ajax响应中获得数组结果。 在这里检查两件事 (row.bookable !=null) and (row.bookable.slug !=null) 这通常是因为“bookable”属性的值为null $.each(res,function(index,row){ if(row.b
您正在使用for each,因此我想您将在ajax响应中获得数组结果。 在这里检查两件事
(row.bookable !=null) and (row.bookable.slug !=null)
这通常是因为“bookable”属性的值为null
$.each(res,function(index,row){
if(row.bookable!=null)
{
console.log(row.bookable);
}
//{id: 2, slug: "dire_international_queen", organization_id: 1, roomtype_id: 5, no_bed: "2", …}
//check here
if(row.bookable !=null && row.bookable.slug!=null)
console.log(row.bookable.slug);
})
您正在使用for each,因此我想您将在ajax响应中获得数组结果。 在这里检查两件事
(row.bookable !=null) and (row.bookable.slug !=null)
这通常是因为“bookable”属性的值为null
$.each(res,function(index,row){
if(row.bookable!=null)
{
console.log(row.bookable);
}
//{id: 2, slug: "dire_international_queen", organization_id: 1, roomtype_id: 5, no_bed: "2", …}
//check here
if(row.bookable !=null && row.bookable.slug!=null)
console.log(row.bookable.slug);
})
res
变量中有一个对象数据列表。您得到错误是因为在查找中的某个地方,您的程序无法找到slug属性
使用hasOwnProperty()
检查属性是否存在,然后应用相关操作
$.each(res,function(index,row){
if ( row.bookable.hasOwnProperty('slug') ) {
console.log(row.bookable.slug);
}
}
查看详细信息您在
res
变量中有一个数据对象列表。您得到错误是因为在查找中的某个地方,您的程序无法找到slug属性
使用hasOwnProperty()
检查属性是否存在,然后应用相关操作
$.each(res,function(index,row){
if ( row.bookable.hasOwnProperty('slug') ) {
console.log(row.bookable.slug);
}
}
查看详细信息了解一下似乎您正在尝试在ajax返回结果之前检索值,而它现在正在工作,它与jquery更新或其他事情有关??没有足够的详细信息来重现问题。它不能同时工作和不工作。请创建一个包含更多相关细节的应用程序,例如如何调用相关ajax请求并获取其结果。似乎您正在尝试在ajax返回结果之前检索值。在它刚刚运行时,它与jquery更新或其他事情有关??没有足够的细节来重现问题。它不能同时工作和不工作。请创建一个包含更多相关细节的应用程序,例如如何调用相关ajax请求并获得其结果