I am consulting two different json data sources, job1 and job2, with the same code that runs with jsontohtml.
Each source has different labels, although with similar name, some have different name beetween them or different json structure.
I want to detect when a label is defined to show it in html, while if is undefined not show it.
The problem is when I check if the corresponding parameter-object is already defined by next command
if (typeof(obj._source.file.url) !== 'undefined')
I get the following error
Uncaught TypeError: Cannot read property 'url' of undefined
the complete code is
<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>
if you comment the code of the following line, which is the one that gives me the error, my jsontohtml code will work without problem
if (typeof(obj._source.file.url) !== 'undefined') { http = obj._source.file.url.replace("file://", ""); return("<a href='" + http + "'>" + http + "</a>"); }; //job 1 source
The source data of the source job1 is
{
"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"""
}
}
}
]
}
}
Thanks in advance