I'm learning Nodejs with Express and Mongo. I watched a tutorial on Youtube and tried to make a simple online store. Everything is fine: I can save name, address, phone, and email of customers who filled in the form to Mongo, but the problem is that I cannot save the correct products' data if there are 2 or more different products. Mongo only receives data of the last product, which you can see in the image below. Here are parts of my code:
// My Order's schema
var orderSchema = Schema({
name: {
type: String,
required: true
},
address: {
type: String,
required: true
},
phone: {
type: String,
required: true
},
email: {
type: String,
},
product: [{
image: String,
title: String,
price: Number,
qty: Number,
}],
/* subtotal: {
type: Number,
required: true
},
total: {
type: Number,
required: true
} */
});
var Order = module.exports = mongoose.model('Order', orderSchema);
// My Product's schema
var productSchema = mongoose.Schema({
title: {
type: String,
required: true
},
slug: {
type: String,
required: true
},
description: {
type: String,
required: true
},
category: {
type: String,
required: true
},
price: {
type: Number,
required: true
},
image: {
type: String,
},
});
module.exports = mongoose.model('Product', productSchema);
<!-- Form for the customers to fill their information -->
<form action="/orders/confirm" method="POST">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<label>Address</label>
<input type="text" class="form-control" name="address" placeholder="Address" required>
</div>
<div class="form-group">
<label>Phone</label>
<input type="tel" class="form-control" name="phone" placeholder="Phone number: 0123456789" pattern="[0-9]{10}" required>
</div>
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email" placeholder="Email">
</div>
<div class="form-group">
<label>Product</label>
<div class="col-xs-12 col-md-5">
<table class="table table-stripped align-mid">
<tr>
<th>Image</th>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th></th>
<th>Subtotal</th>
</tr>
<% var total = 0; %>
<% cart.forEach((product) => { %>
<% var sub = product.qty * product.price; %>
<% total += +sub; %>
<tr>
<td><img class="cpi" src="<%= product.image %>" alt="<%= product.slug %>"></td>
<td><%= product.title %></td>
<td><%= product.price %> VND</td>
<td><%= product.qty %></td>
<td></td>
<td><%= sub %> VND</td>
</tr>
<% }); %>
<tr>
<td colspan="5" align="right"><b>Total: </b><%= total %> VND</td>
</tr>
</table>
</div>
</div>
<button type="submit" class="btn btn-primary">Confirm Order</button>
</form>
// Routing
router.post('/confirm', (req,res) => {
var name = req.body.name;
var address = req.body.address;
var phone = req.body.phone;
var email = req.body.email;
var cartData = req.session.cart;
req.checkBody('name', 'Name is required!').notEmpty();
req.checkBody('address', 'Address is required!').notEmpty();
req.checkBody('phone', 'Phone number is required!').notEmpty();
var errors = req.validationErrors();
if (errors) {
res.render('order_info', {
errors: errors,
cart: cartData
});
} else {
Order.findOne((err,order) => {
if (err) {
res.render('order_info', {
errors: errors,
cart: cartData
});
} else {
var total = 0;
cartData.forEach((product) => {
var sub = product.qty * product.price;
total += +sub;
order = new Order({
name: name,
address: address,
phone: phone,
email: email,
product: [{
image: product.image,
title: product.title,
price: product.price,
qty: product.qty,
subtotal: sub,
}],
total: total
});
});
order.save((err) => {
if (err) {
console.log(err);
} else {
console.log(order);
req.flash('success','Order Confirmed.');
cartData.length = 0;
res.redirect('/products/all')
}
});
}
});
}
});
(I add 2 products to cart, the first product's price is 5000000, and the second one is 3500000)
I've tried many ways but nothing works. Please help! Thank you in advance.
