Node.js 是否在EJS模板上显示嵌套的猫鼬数据?

Node.js 是否在EJS模板上显示嵌套的猫鼬数据?,node.js,mongodb,express,mongoose,ejs,Node.js,Mongodb,Express,Mongoose,Ejs,我试图显示嵌套mongoose对象的订单数据,但似乎无法正确显示数据。提前感谢所有的帮助 以下是订单模型: 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'}, ca

我试图显示嵌套mongoose对象的订单数据,但似乎无法正确显示数据。提前感谢所有的帮助

以下是订单模型:

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>
这是购物车:

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;
    };
};

cart:{type:Object,required:true}
是一个错误。这不应该是
对象
,但事实上,您可能想要成为一个“数组”。不知何故,您设法将“应该”是数组的内容强制转换为“命名键”的对象。但是,由于您使用了
对象
,因此实际数据被持久化为“字符串”,实际上根本不是
对象
。在“最坏”的情况下,你指的是混合的,它做了一些不同的事情。但它实际上“应该”是一个数组,我把它改成了数组类型,现在数据保存在我添加的编辑中,如上图所示。我还添加了购物车供上面参考。我似乎仍然无法获得具体的数据,比如说标题。我可以在ejs中尽可能多地向我展示这些项目,但无法深入,我需要添加什么才能达到标题级别?非常感谢你的帮助。
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 }