My question is about to show error messages in nodejs, mysql, ejs billing app, my concern is I am having form code which has submit action as "/addbill" and this addbill has already logic to delete and insert records into db and its corresponding ejs template to render is printbill.ejs.
Now when I submit the form by entering the quantity text box value as 0, so when I submit I should show error message that "it should not accept value as 0", I have written logic here in server.js below
If Logic below having "viewbill.ejs" to render with errormessage object, problem here is this ejs already having products listed from database and it has data binded as products array, so when I pass the errormessage I am getting error that there is no products and showing undefined even If I add products as the blank array as this time I am not getting error and I successfully seeing the error but my products go away. Actually I need to retain the products value as well as to show this error on top of it.
So in this case do I need to call again the products query to get it from DB and combine along with error object to achieve this. Because I need to show only the error message but wanted to avoid unnecessary DB call. Any advice would help me on this?
I am also aware that achieving this with client side validation is possible but since I am new to technology I want to know whether any approach that would help to show errors across all the pages as generic by having partial error templates that can be included wherever necessary.
app.post('/addbill',checkAuthenticated,(req,res) => {
let mon=parseInt(new Date().getMonth())+1
let currdate=new Date().getDate()+"/"+mon+"/"+new Date().getFullYear()
let currtime=new Date().toLocaleTimeString()
let ob = req.body;
for (var propName in ob) {
if (propName === null || propName === undefined || propName === "" || propName==="[object Object]") {
delete ob[propName];
}
}
let formdata = JSON.stringify(ob)
formdata=formdata.replace(/\\/g,"").replace(/"\[{/g,"").replace(/\]":""}$/,"")
formdata="["+formdata+"]"
let parsedata=JSON.parse(formdata)
let dbdata=[]
let ln = Math.floor(Math.random() * 9000) + 1000;
let billnumber="INV-".concat(currdate.replace(/\//g,"")).concat("-"+ln);
for (var i=0;i<parsedata.length;i++) {
dbdata.push([currdate,currtime,billnumber,parsedata[i].itemname,parsedata[i].qty,parsedata[i].price])
}
let errormessage="Quantity value should not be zero";
**if(parseInt(parsedata[0].price)===0) {
console.log('error message'+errormessage)
return res.render('viewbill.ejs',{errormessage:errormessage});
}**
let sql = `INSERT INTO billdb(BillDate,BillTime,BillNumber,ItemName,Quantity,Price) VALUES ? `
let query = mysqlConnection.query(sql,[dbdata],(err, rows, fields) => {
if(!err)
{
let sql1=`SELECT * FROM billdb WHERE BillNumber='${billnumber}'`
let query1 = mysqlConnection.query(sql1, (err, rows1, fields) => {
if(!err)
{
let printbill=rows1
res.render('printbill.ejs',{printbill:printbill})
}
else
console.log(err)
})
}
else
console.log(err)
})
})
//this is the logic to show error message
if(parseInt(parsedata[0].price)===0) {
console.log('error message'+errormessage)
return res.render('viewbill.ejs',{errormessage:errormessage});
}```
>asfd