Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS Better way of handling undefined objects in Nodejs

Kings

New Coder
I want to skip all undefined values and proceed with next value. I used hasOwnProperty function, however want to know best way to handle undefined object. Below is my code.

const md = require ('md-2-json');
const fs = require ('fs');
const xlsx = require ('xlsx');

var Headers = ['ChangeId', 'Issue No','Incident','Change Description', 'Change Date (MM/DD/YYYY)', 'ExcutorTeam','DownStream Dependency','Change Type'];

let htmlobj = '<h2>Change Details Report</h2>';

fs.readFile('changedetails.json',function(err, data){
if(err){
htmlobj += '<table><tr>change Details not generated</tr></table>';
}
else if(data !== ' '){
htmlobj += '<table><tr>';
htmlobj += '<h3>Change Details:</h3></tr><tr>';
for(let i=0;i<Headers.length;i++){
htmlobj += '<td>'+Headers+'</td>';
}
htmlobj += '</tr>'
jdataparse = JSON.parse(data);
for(let i=0;i<jdataparse.length;i++){
htmlobj += '<tr>';
var markdownCD = md.parse(jdataparse.description);
var CD = markdownCD.hasOwnProperty['Change Details'];
var CDData = [jdataparse.id, CD['Issue No'], CD['Incident'], CD['Change Description'],CD['Change Date (MM/DD/YYYY)'],CD['Implementer Team'],CD['DownStream Impact'], CD['Change Type']]
var CDDetails =[];
var formatedString =[];
for(let j=0;j<CDData.length;j++){
if(typeof(JSON.stringify(CDData[j])) !== 'undefined'){
if(JSON.stringify(CDData[j]).includes("[x]")){
formatedString[j] = stringOps(JSON.stringify(CDData[j]));
var str = formatedString[j].split("-",formatedString[j].length);
for(let t=0;t<str.length;t++){
if(str[t].includes("[x]")){
var data1 = str[t];
CDDetails[j] = data1.replace("[x]", " ").trim();
}
}
} else {
CDDetails[j] = stringOps(JSON.stringify(CDData[j]))
}
}
htmlobj += '<td>'+CDDetails[j]+'</td>';
}
htmlobj += '</tr>'
}
htmlobj += '</table><br>'
}
setTimeout(() => {callBack();}, 1000);
});

function callBack(){
htmlobj += '</table><br><br>';
const stream = fs.createWriteStream("changedetails.html");
stream.write(`<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"/><title>Change Details</title>`)
stream.write(`<style>table{
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
th{
border: 2px solid black;
text-align: left;
padding: 8px;
}
td, tr { border: 2px solid black; }
</style>`)
stream.write(htmlobj)
stream.close();
}
function stringOps(str){
parameterstr = str;
return parameterstr.trim().split('\\n').join(' ').replace('{',' ').replace('}',' ').replace('"}',' ').replace('"raw":"',' ').replace('"',' ')
}


changedetails.json files

[{
"id": 125,
"iid": 1256,
"title": "Bug fix - Handled null Exception, while Address is null ",
"description": "# Change Details #\n\n ## Issue No: ## 123 \n## Incident: ##\n INC123 \n## Change Description ##This is test change\n## Change Date (MM/DD/YYYY) ##\n05/15/2022\n## Implementer Team ##\nTiger Team\n## DownStream Impact ##\nNo\n## Change Type ##\n\n- [x] Minor\n- [ ] Major\n- [ ] Emergency",
"mr_link": "https://github.com/mr-requests/1234",
"commit_sha": "de3b4g9g329449cdge032203d"
}
]
changedetails1.json
[{
"id": 123,
"iid": 1234,
"title": "Bug fix - Handled null Exception, while Address is null ",
"description": "# Change Details #\n\n ## Issue No: ##\n 123 \n## Incident: ##\n INC123 \n## Change Description ##\nThis is test change\n## Change Date (MM/DD/YYYY) ##\n05/15/2022\n## Implementer Team ##\nTiger Team\n## DownStream Impact ##\nNo\n## Change Type ##\n\n- [x] Minor\n- [ ] Major\n- [ ] Emergency",
"mr_link": "https://github.com/mr-requests/1234",
"commit_sha": "de3b4g9g329449cdge032203d"
}
]
 
Back
Top Bottom