我正在尝试在HTML页面上加载一个向我返回JSON
的函数以创建表。
有没有一种方法可以启动该函数,并将返回的JSON
主体插入到HTML
中存在的变量中,以便能够将其转换为{{1} }?
odatafunction.js
table
index.js
let Request = require("request");
const EventEmitter = require("events").EventEmitter;
const risultato = new EventEmitter();
let keys;
function createArrayKey(array, object) {
let key = Object.keys(object);
for (let i = 0; i < Object.keys(object).length; i++) {
if(i === 0){
array += object[key[i]];
}else{
array += "," + object[key[i]];
}
}
return array;
}
function removeDuplicates(array, key) {
let lookup = {};
let result = [];
array.forEach(element => {
if (!lookup[element[key]]) {
lookup[element[key]] = true;
result.push(element);
}
});
return result;
}
function check_missing_value(new_object2, max_key) {
var breakLoop = false;
var miss;
for(var i=0; i<Object.keys(new_object2).length ; i++){
miss = -1;
for(var j = 0; j < Object.keys(max_key).length && !breakLoop ;j++){
//console.dir(max_key[j]);
if(!Object.keys(new_object2).includes(max_key[j])){
new_object2[max_key[j]]="NULL";
breakLoop = true;
}
}
}
return new_object2;
}
function retriveData(){
const http = Request("https://services.odata.org/V3/OData/OData.svc/PersonDetails?$format=json",
(error, response, body) => {
if (error) {
return console.dir(error);
}
let json_body = JSON.parse(body);
let value_json = json_body.value;
let new_json = [];
var html = '<table class="table table-striped">';
html += '<tr>';
//console.dir(Object.keys(value_json[0]));
let max_key = (Object.keys(value_json[0]));
keys = max_key;
for (var i = 0; i < Object.keys(keys).length; i++) {
html += '<th>' + keys[i] + '</th>';
}
html += '</tr>';
for (let i = 0; i < Object.keys(value_json).length; i++) {
let new_object2 = {};
let element = value_json[i];
html += '<tr>';
for (var j = 0; j < Object.keys(element).length; j++) {
var temp = Object.keys(element);
switch (typeof element[temp[j]]) {
case "object":
var array = createArrayKey("", element[temp[j]]);
new_object2[temp[j]] = array;
html += '<td>' + array + '</td>';
break;
case "number":
new_object2[temp[j]] = element[temp[j]];
html += '<td>' + element[temp[j]] + '</td>';
break;
case "string":
new_object2[temp[j]] = element[temp[j]];
html += '<td>' + element[temp[j]] + '</td>';
break;
case "boolean":
new_object2[temp[j]] = element[temp[j]];
html += '<td>' + element[temp[j]] + '</td>';
break;
case "bigint":
new_object2[temp[j]] = element[temp[j]];
html += '<td>' + element[temp[j]] + '</td>';
break;
}
}
html += '<tr>';
new_object2 = check_missing_value(new_object2, max_key);
new_json.push(new_object2);
}
html += '</table>';
removeDuplicates(new_json, 'PersonID');
risultato.data = new_json;
risultato.emit('update');
console.dir(html);
});
risultato.on('update', function () {
//console.log(risultato.data); // HOORAY! THIS WORKS!
});
return risultato.data;
}
define({
results : retriveData()
});
module.exports.retriveData = retriveData;
index.ejs
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
//res.sendFile('index.js');
});
module.exports = router;