我正在尝试从嵌套的猫鼬对象中显示订单数据,而我似乎无法正确显示数据。提前感谢所有帮助。
以下是订单型号:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var schema = new Schema({
//todo: check the user part in this model
user: {type: Schema.Types.ObjectId, ref: 'User'},
cart: {type: Object, required: true},
address: {type: String, required: true},
name: {type: String, required: true},
paymentId: {type: String, required: true},
email:{type: String, required: true},
time : { type : Date, default: Date.now }
});
module.exports = mongoose.model('Order', schema);
以下是数据保存到数据库的方式:
{ "_id" : ObjectId("5975214fc8db300731d0e8b8"), "user" : ObjectId("5951d4d1f57b440556898e1a"), "cart" : { "items" : { "596068
4581c02d077c914283" : { "item" : { "_id" : "5960684581c02d077c914283", "imagePath" : "https://www.swarovski.com/is-bin/inters
hop.static/WFS/SCO-Media-Site/-/-/publicimages//CG/B2C/PROD/180/Swarovski-Cosmic-Bracelet-5226308-W180.jpg", "title" : "Brace
let 1", "description" : "This is the first Bracelet in the collection.", "price" : 10, "__v" : 0 }, "qty" : 1, "price" : 10 }
, "59742cad8f1bf6071b5419cb" : { "item" : { "_id" : "59742cad8f1bf6071b5419cb", "title" : "Bracelet 2 ", "imagePath" : "https
://img0.etsystatic.com/160/0/12655872/il_340x270.1187191078_i2ha.jpg", "description" : "This is the second Bracelet in the co
llection.", "price" : 5, "__v" : 0 }, "qty" : 1, "price" : 5 }, "59742ebdd1242f07530d2b30" : { "item" : { "_id" : "59742ebdd1
242f07530d2b30", "title" : "Bracelet 3", "imagePath" : "https://www.costco.com/wcsstore/CostcoUSBCCatalogAssetStore/category-
tiles/pearl-bracelets.jpg", "description" : "This is the third Bracelet in the collection.", "price" : 12, "__v" : 0 }, "qty"
: 1, "price" : 12 } }, "totalQty" : 3, "totalPrice" : 27 }, "address" : "6210 place", "name" : "pauls", "paymentId" : "ch_1A
isRfDfJryYeuMpbJGWY2NV", "email" : "pauls@a.com", "time" : ISODate("2017-07-23T22:21:03.819Z"), "__v" : 0 }
我想使用ejs在表格中显示数据:
<table border="1">
<tr>
<th>Item</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
</tr>
<% orders.forEach(function(order){ %>
<tr>
<td><%=...item%> </td>
<td><%=...description%> </td>
<td><%=...price%> </td>
<td><%=...quantity%> </td>
</tr>
<% }); %>
</table>
这是路线:
router.get('/order', function(req, res){
Order.find({}, function(err, allOrders){
if(err){
console.log(err);
} else {
res.render("admin/order", {orders: allOrders});
}
});
});
这是购物车:
module.exports = function Cart(oldCart) {
this.items = oldCart.items || {};
this.totalQty = oldCart.totalQty || 0;
this.totalPrice = oldCart.totalPrice || 0;
this.add = function (item, id) {
var storedItem = this.items[id];
if (!storedItem) {
storedItem = this.items[id] = {item: item, qty: 0, price: 0};
}
storedItem.qty++;
storedItem.price = storedItem.item.price * storedItem.qty;
this.totalQty++;
this.totalPrice += storedItem.item.price;
};
this.reduceByOne = function (id) {
this.items[id].qty--;
this.items[id].price -= this.items[id].item.price;
this.totalQty--;
this.totalPrice -= this.items[id].item.price;
if (this.items[id].qty <=0) {
delete this.items[id];
}
};
this.removeItem = function (id) {
this.totalQty -= this.items[id].qty;
this.totalPrice -= this.items[id].price;
delete this.items[id];
};
this.generateArray = function () {
var arr = [];
for (var id in this.items) {
arr.push(this.items[id]);
}
return arr;
};
};
编辑:现在数据保存如下:
{ "_id" : ObjectId("5976b6b11306910658b1ff57"), "address" : "6210 place", "name" : "frank", "paymentId" : "ch_1AjJRVDfJ
ryYeuMpJC80cp5k", "email" : "g@mail.com", "time" : ISODate("2017-07-25T03:10:41.522Z"), "cart" : [ { "items" : { "59752
28a215c0f074b64f58e" : { "item" : { "_id" : "5975228a215c0f074b64f58e", "title" : "Bracelet 3", "imagePath" : "https://
www.costco.com/wcsstore/CostcoUSBCCatalogAssetStore/category-tiles/pearl-bracelets.jpg", "description" : "This is brace
let 3", "price" : 12, "__v" : 0 }, "qty" : 1, "price" : 12 }, "59752242215c0f074b64f58c" : { "item" : { "_id" : "597522
42215c0f074b64f58c", "title" : "Bracelet 1", "imagePath" : "https://img0.etsystatic.com/160/0/12655872/il_340x270.11871
91078_i2ha.jpg", "description" : "This is bracelet 1", "price" : 10, "__v" : 0 }, "qty" : 2, "price" : 20 }, "5975226a2
15c0f074b64f58d" : { "item" : { "_id" : "5975226a215c0f074b64f58d", "title" : "Bracelet 2", "imagePath" : "http://media
.tiffany.com/is/image/Tiffany/EcomBrowseM/paloma-picasso-knot-bead-bracelet-34946183_963148_ED.jpg?op_usm=1.00,1.00,6.0
0&defaultImage=NoImageAvailable&&", "description" : "This is bracelet 2", "price" : 5, "__v" : 0 }, "qty" : 1, "price"
: 5 } }, "totalQty" : 4, "totalPrice" : 37 } ], "__v" : 0 }