-1

I am trying to fetch user info through a form and then insert same data in the table 'orders'. Everything looks correct, but there is an error showing total_cost is undefined. I tried declaring it globally as well.

total_cost should be the quantity selected by the user in the form multiplied by the price of the product which is fetched from the database.

Here is the app.js post request:

    // post create_order page
app.post("/create_order", function(req, res){
    var name = req.body.name;
    var quantity = req.body.quantity;
    var email = req.body.email;

    // fetching price from menu table
    var sql = "select Price from menu where Name=?";
    db.query(sql,[name], function(err, result){
        if(err){
            throw err;
        }else if(result==0){
            console.log("No items found!!!")
        }else{
            console.log(result)
            console.log(Price)
            var total_cost = result;
            console.log(total_cost  )
        }
    })

    // Updating the order table
    let today = new Date().toISOString().slice(0, 10);
    let order = {
        Item : name,
        Quantity : quantity,
        TotalCost : total_cost,
        Date : today,
        email : user
    }
    sql1 = "Insert into orders SET ?";
    db.query(sql1, order, function(err, result2){
        if(err){
            throw err;
        }else{
            res.redirect("/orders");
        }
    })
})

Here is the corresponding order form used to fetch data:

    <!-- //Order form goes here -->
  <div class="orderform"> 
    <form action="/create_order" method="POST">
      <div class="form-group">
        <label for="exampleFormControlSelect1">Select Item</label>
        <select class="form-control" name="name" id="exampleFormControlSelect1">
          <%result.forEach(function(result){%>
            <option><%=result.Name%></option>
          <%})%>
    
        </select>
      </div>
      <div class="form-group" >
        <label for="exampleFormControlInput1">quantity</label>
        <input type="number" name="quantity" class="form-control" id="exampleFormControlInput1" placeholder="quantity" min="1" max="10">
      </div>
      <div class="form-group">
        <label for="exampleFormControlInput1">Email address</label>
        <input type="email" name="email" class="form-control" id="exampleFormControlInput1" placeholder="name@example.com">
      </div>

Screenshot of error page: Error page I am getting upon submitting the form

Martheen
  • 5,198
  • 4
  • 32
  • 55
dutta05
  • 1
  • 2
  • Does this answer your question? [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Martheen Jan 16 '21 at 05:16

1 Answers1

0

You defined your variable inner-scope; this will cause issues!

                let profile = 20;

                function call() {
                    console.log(profile); // == 20
                    let profile2 = 26;
                }
                call();
                console.log(profile2); // undefined in other words your error.

If it's not a scope issue, then it's certainly the data you're grabbing from data-base is incorrect. Double check the query.

BGPHiJACK
  • 1,277
  • 1
  • 8
  • 16
  • Assuming your console.log on the data, was AOK to pass-along no issues there or "undefined" error; try defining your total_cost variable just below email. var total_cost = undefined; that should give it global scope like email. – BGPHiJACK Jan 16 '21 at 05:43