-1

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
5
  • 2
    You're showing a lot of dense code, with non-code formatting munged into it, which makes it difficult to read and follow. Can you improve the formatting of the code? Additionally, can you clarify the specific operation which is producing an error and what you are asking about that error? So far your question is simply, "Is it possible to write logic without using a database?" The answer to that is of course "Yes", but presumably that answer alone does not help you. Please clarify specific debugging information about the problem you are trying to solve. Commented Nov 11 at 12:41
  • 3
    I’m not sure what the question is. Commented Nov 11 at 13:20
  • Is it correct that: You have a website with a form and you want to show possible errors to the user after form submission? And: You don't want to use client side form validation because you don't have much experience using client side JS (hence the need for showing errors after submission)? Because usually the latter approach is used (showing errors as early as possible to the user), but maybe there's another reason for not wanting client side validation besides unfamiliarity (for example you need to validate a submitted value against data that the client does and should not have). Commented Nov 11 at 14:57
  • 3
    N.B. Even if client-side validation is an option for you, it can be bypassed very easily, so you must still have server-side validation in place as well to ensure data integrity and security. So you cannot consider client-side validation as an alternative to this, only as an added enhancement. Commented Nov 11 at 15:25
  • Thanks, I will try to reconstruct my requirements clearly Commented Nov 13 at 3:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.