As part of my order processing, I am saving the order details into a JSON file named order_details.json
. Here is an example of how the data is structured:
[{
"uniqueID": "CHECKOUT_IE01",
"orderID": "4001820182",
"date": "06-02-2019 16:55:32.321",
"cartTotal": "€19.98"
}, {
"uniqueID": "CHECKOUT_DK01",
"orderID": "6001825057",
"date": "06-02-2019 16:56:15.976",
"cartTotal": "318 DKK"
}]
Now, I want to create an HTML report that will display all this data in a formatted way.
I'm currently unsure about which technology to use for this task. So far, I have attempted to write some JavaScript code as shown below:
var fs = require(['fs']);
var data = fs.readFileSync("D:\\order-detail.json", "utf8");
var data1 = JSON.parse(data);
console.log(data1);
var unique_id = data1[0].uniqueID;
var order_id = data1[0].orderID;
var order_date = data1[0].date;
var cart_total = data1[0].cartTotal;
document.getElementById("uid").innerHTML = unique_id;
document.getElementById("oid").innerHTML = order_id;
document.getElementById("date").innerHTML = order_date;
document.getElementById("ctotal").innerHTML = cart_total;
and in the HTML file:
<body onload="myFunction()">
<h3>Values from Json</h3>
<div>
<span id="uid"></span>
<span id="oid"></span>
<span id="date"></span>
<span id="ctotal"></span>
</div>
However, I am encountering an error and the functionality is not working as expected. The error message displayed in the console is:
dashboard.html:8 Uncaught TypeError: fs.readFileSync is not a function
at myFunction (dashboard.html:8)
at onload (dashboard.html:25)
myFunction @ dashboard.html:8
onload @ dashboard.html:25
require.js:5 GET file:///D:/JSON/fs.js net::ERR_FILE_NOT_FOUND
req.load @ require.js:5
load @ require.js:5
load @ require.js:5
fetch @ require.js:5
check @ require.js:5
enable @ require.js:5
enable @ require.js:5
(anonymous) @ require.js:5
(anonymous) @ require.js:5
each @ require.js:5
enable @ require.js:5
init @ require.js:5
(anonymous) @ require.js:5
setTimeout (async)
req.nextTick @ require.js:5
o @ require.js:5
requirejs @ require.js:5
myFunction @ dashboard.html:7
onload @ dashboard.html:25
require.js:5 Uncaught Error: Script error for "fs"
http://requirejs.org/docs/errors.html#scripterror
at makeError (require.js:5)
at HTMLScriptElement.onScriptError (require.js:5)
I am seeking assistance as to what might be wrong in my approach or if there is a better way to achieve this task. Any help would be greatly appreciated!