我正在使用与 jsontohtml 一起运行的相同代码咨询两个不同的 json 数据源 job1 和 job2。
每个来源都有不同的标签,虽然名称相似,但有些来源之间有不同的名称或不同的 json 结构。
我想检测何时定义标签以在 html 中显示它,而如果未定义则不显示它。
问题是当我检查下一个命令是否已经定义了相应的参数对象时
if (typeof(obj._source.file.url) !== 'undefined')
我收到以下错误
Uncaught TypeError: Cannot read property 'url' of undefined
完整的代码是
<html>
<head>
<meta charset='UTF-8' />
<title>JSON Visualizer</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/json2html/1.4.0/json2html.min.js"></script>
</head>
<link rel="stylesheet" href="./tablaX.css">
<body>
<div class="container">
<div class="table-responsive">
<div id="elastic_table">
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
var trabajo = "job2";
var url = 'http://localhost:9200/' + trabajo + '/_search'
let transforms = { "response":
{'<>':'table','class':'table-bordered table-striped table-hover results','html':[
{'<>':'thead'},
{'<>':'tr'},
{'<>':'th','text':'URL'},
{'<>':'tbody'},
{"<>":"div","html":function() {
return($.json2html(this.hits.hits,transforms.hitos));
}}
]},
"hitos":{"<>":"tr","html":[
{"<>":"td","html":function(obj,index){
if (typeof(obj._source.file.url) !== 'undefined') { http = obj._source.file.url.replace("file://", ""); return("<a href='" + http + "'>" + http + "</a>"); }; //job 1 source
if (typeof(obj._source.url) !== 'undefined') { http = obj._source.url.replace("file://", ""); return("<a href='" + http + "'>" + http + "</a>"); }; //job 2 source
}
}
]}
};
var datos =
{
"query": {
"match_all": {}
},
"size": "10000"
}
;
[datos].map(function (query) {
$.ajax({
url,
type: "post",
data: JSON.stringify(query),
contentType: "application/json",
dataType: "json",
success: function (responses) {
console.log({ query, responses });
$('#elastic_table').html(json2html.transform(responses,transforms.response));
},
error: function (err) {
console.error({ query, err });
}
});
});
}
)
;
</script>
</body>
</html>
如果您注释以下行的代码,这是给我错误的代码,我的 jsontohtml 代码将毫无问题地工作
if (typeof(obj._source.file.url) !== 'undefined') { http = obj._source.file.url.replace("file://", ""); return("<a href='" + http + "'>" + http + "</a>"); }; //job 1 source
源job1的源数据是
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "job1",
"_type" : "_doc",
"_id" : "2e2ed07786c01e9fd845b62fb86a693a",
"_score" : 1.0,
"_source" : {
"content" : """hola
""",
"meta" : { },
"file" : {
"extension" : "txt",
"content_type" : "text/plain; charset=ISO-8859-1",
"created" : "2020-04-03T11:57:45.673+0000",
"last_modified" : "2020-04-23T07:33:50.725+0000",
"last_accessed" : "2020-04-23T07:33:50.725+0000",
"indexing_date" : "2020-04-23T10:33:46.954+0000",
"filesize" : 4,
"filename" : "fichero0_borrador.txt",
"url" : """file://C:\TMPM\carpeta0\99.Borrador\fichero0_borrador.txt"""
},
"path" : {
"root" : "4cface21f575d5e5985e4e3889c278a3",
"virtual" : "PM/carpeta0/99.Borrador/fichero0_borrador.txt",
"real" : """C:\TMPM\carpeta0\99.Borrador\fichero0_borrador.txt"""
}
}
},
{
"_index" : "job1",
"_type" : "_doc",
"_id" : "6f475ff7ac21fc59f4edc34761d09",
"_score" : 1.0,
"_source" : {
"content" : """cadena0 A
""",
"meta" : { },
"file" : {
"extension" : "txt",
"content_type" : "text/plain; charset=ISO-8859-1",
"created" : "2020-04-14T09:30:20.218+0000",
"last_modified" : "2020-04-14T09:46:02.930+0000",
"last_accessed" : "2020-04-14T09:46:02.930+0000",
"indexing_date" : "2020-04-23T10:33:47.533+0000",
"filesize" : 9,
"filename" : "fichero0_A.txt",
"url" : """file://C:\TMPM\carpeta0\fichero0_A.txt"""
},
"path" : {
"root" : "c073f96a8d16ebb2e604bebd4fe8b40",
"virtual" : "PM/carpeta0/fichero0_A.txt",
"real" : """C:\TMPM\carpeta0\fichero0_A.txt"""
}
}
},
{
"_index" : "job1",
"_type" : "_doc",
"_id" : "7dfb102f2e64e99740a89b1cff7d92a3",
"_score" : 1.0,
"_source" : {
"content" : """cadena1
""",
"meta" : { },
"file" : {
"extension" : "txt",
"content_type" : "text/plain; charset=ISO-8859-1",
"created" : "2020-04-03T11:56:21.165+0000",
"last_modified" : "2020-04-03T12:23:23.872+0000",
"last_accessed" : "2020-04-03T12:23:23.872+0000",
"indexing_date" : "2020-04-23T10:33:47.568+0000",
"filesize" : 7,
"filename" : "fichero1.txt",
"url" : """file://C:\TMPM\carpeta1\fichero1.txt"""
},
"path" : {
"root" : "7ff4e7b0e0c3172dbd29a23861ab2125",
"virtual" : "PM/carpeta1/fichero1.txt",
"real" : """C:\TMPM\carpeta1\fichero1.txt"""
}
}
},
{
"_index" : "job1",
"_type" : "_doc",
"_id" : "74f0e8c1cccf2f32b4d43673cfdca0",
"_score" : 1.0,
"_source" : {
"content" : """cadena2
""",
"meta" : { },
"file" : {
"extension" : "txt",
"content_type" : "text/plain; charset=ISO-8859-1",
"created" : "2020-04-03T11:56:30.524+0000",
"last_modified" : "2020-04-03T12:23:35.029+0000",
"last_accessed" : "2020-04-03T12:23:35.029+0000",
"indexing_date" : "2020-04-23T10:33:47.579+0000",
"filesize" : 7,
"filename" : "fichero2.txt",
"url" : """file://C:\TMPM\carpeta2\fichero2.txt"""
},
"path" : {
"root" : "fd0c8ae8a885725cfcaee774c72be43",
"virtual" : "PM/carpeta2/fichero2.txt",
"real" : """C:\TMPM\carpeta2\fichero2.txt"""
}
}
},
{
"_index" : "job1",
"_type" : "_doc",
"_id" : "8d6c56c0448bc4bfa17787923baa9c",
"_score" : 1.0,
"_source" : {
"content" : """cadenaX
""",
"meta" : { },
"file" : {
"extension" : "txt",
"content_type" : "text/plain; charset=ISO-8859-1",
"created" : "2020-04-14T15:12:56.632+0000",
"last_modified" : "2020-04-14T15:34:53.139+0000",
"last_accessed" : "2020-04-14T15:34:53.139+0000",
"indexing_date" : "2020-04-23T10:33:47.587+0000",
"filesize" : 7,
"filename" : "fichero_X.txt",
"url" : """file://C:\TMPM\carpetaX\fichero_X.txt"""
},
"path" : {
"root" : "f43118a935c6c7ef81ea65307759fc",
"virtual" : "PM/carpetaX/fichero_X.txt",
"real" : """C:\TMPM\carpetaX\fichero_X.txt"""
}
}
}
]
}
}
提前致谢